Bitte beachten Sie die Modelle unter:Geschützte Variablen und Modell Bindung
public class Apple //: Fruit
{
public string Description { get; set; }
public int Id { get; protected set; }
}
public class AppleModel
{
public int Id { get; set; }
public string Description { get; set; }
}
und Controller unter:
[HttpPost]
public ActionResult Index(Apple apple)
{
return View();
}
[HttpGet]
public ActionResult Index()
{
var AppleModel = new AppleModel();
AppleModel.Id = 1;
AppleModel.Description = "Apple";
var Apple = AutoMapper.Mapper.Map<Apple>(AppleModel);
return View("View1",Apple);
}
und die Sicht unter:
@model PreQualification.Web.Controllers.Apple
@{
ViewBag.Title = "View1";
}
<h2>View1</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>AppleModel</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Id)
<div class="col-md-10">
@Html.EditorFor(model => model.Id)
@Html.ValidationMessageFor(model => model.Id)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Apple.Id 0 in der HttpPost-Methode, da es sich um eine geschützte Variable im Modell handelt. Gibt es das überhaupt?
Der Grund, warum ich frage, ist, weil ich versuche, dies als Modell zu verwenden: https://github.com/nhibernate/NHibernate.AspNet.Identity/tree/master/source/NHibernate.AspNet.Identity und die ID in der Oberklasse enthalten ist geschützt.
@David, ich habe meinen Beitrag aktualisiert. Es tut uns leid. – w0051977
Warum verwenden Sie nicht auch 'AppleModel' in der Post-Methode? – adiga