C# MVC5-视图不会在post上返回我的对象的Id
我的看法如下:C# MVC5-视图不会在post上返回我的对象的Id,c#,post,asp.net-mvc-5,C#,Post,Asp.net Mvc 5,我的看法如下: @model KtembRegistry.Models.Entities.Member @{ ViewBag.Title = "Üye Güncelleme"; } <h2>@ViewBag.Title</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() @Html.ValidationSummary(true, "", new { @class = "tex
@model KtembRegistry.Models.Entities.Member
@{
ViewBag.Title = "Üye Güncelleme";
}
<h2>@ViewBag.Title</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.MemberId)
@Html.HiddenFor(model => model.Director.EmployeeId);
<div class="form-horizontal">
<hr />
<div class="form-group">
<input type="button" value="Back to List" onclick="location.href='@Url.Action("Index", "Members")'" class="btn btn-primary" />
<input type="submit" value="Save" class="btn btn-success" />
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
@Html.LabelFor(model => model.MemberName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.MemberName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MemberName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Location, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Location, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Location, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
@Html.LabelFor(model => model.MemberClass, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.MemberClass, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MemberClass, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ClassId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ClassId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ClassId, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
@Html.LabelFor(model => model.LastApprovalDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastApprovalDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastApprovalDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TaxNo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TaxNo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TaxNo, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
@Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Fax, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Fax, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Fax, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
@Html.LabelFor(model => model.Mobile, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Mobile, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Mobile, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<br />
<h4>Direktör</h4>
<hr />
<div class="row">
<div class="col-md-6">
<div class="form-group">
@Html.LabelFor(model => model.Director.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Director.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Director.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Director.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Director.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Director.LastName, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
@Html.LabelFor(model => model.Director.IdentityNo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Director.IdentityNo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Director.IdentityNo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Director.Responsibility, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Director.Responsibility, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Director.Responsibility, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<br />
<h3> Personel </h3>
<hr />
<table class="table table-hover">
<thead>
<tr>
<th>İsim</th>
<th>Kimlik No</th>
<th>Yetki</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Employees)
{
<tr>
<td>@item.FirstName</td>
<td>@item.IdentityNo</td>
<td>@item.Responsibility</td>
<td>
@Html.ActionLink("Edit", "Edit", new { @item.EmployeeId }) |
@Html.ActionLink("Delete", "Delete", new { @item.EmployeeId })
</td>
</tr>
}
</tbody>
</table>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
当我发帖时,除了model.Director.EmployeeId之外,所有的东西都被发送到了控制器。控制器将EmployeeId字段正确发送到该字段。不管怎样,我做了,它不起作用。你知道为什么我的帖子不起作用吗?我有一个老习惯,就是让我的实体中的身份字段设置器受到保护,以保护外部混乱。但是,在本例中,它导致标识字段无法更新,这是问题的原因。您确定,您的价值在发布前已交付查看?是的,我非常确定delivered@MiroslavHolec只有未发布的ID您应该删除您的问题-没有提及您的模型及其属性,因此这对任何人都没有用处。@StephenMuecke我无法删除它,因为有答案。。。但我会编辑它
public class Employee
{
public virtual int Id { get; protected set; }
public virtual int EmployeeNo { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual int IdentityNo { get; set; }
public virtual string Responsibility { get; set; }
public virtual Member Company { get; set; }
}