2015-08-25 33 views
0

我的看法回報我對象的ID如下:MVC5 - 視圖無法在後

@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") 
} 

實體:

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; } 

    } 

當我這樣做後,一切都被髮送到控制器就好了除了model.Director.EmployeeId。控制器將EmployeeId字段正確地發送到該字段。無論如何,我這樣做都行不通。任何想法,爲什麼我的帖子不工作?

+1

您確定,您的價值在發佈之前已交付給View嗎? –

+0

是的,我很確定它是交付 – Cemre

+0

@MiroslavHolec這只是沒有公佈的ID – Cemre

回答

-1

看起來你有兩個輸入字段中的員工ID。嘗試移除此隱藏字段

@Html.HiddenFor(model => model.Director.EmployeeId); 

如果您在相同頁面中的某個位置顯示它,則隱藏某些內容並不合理。

編輯

,也可以是你可以刪除後一個(假設其主鍵,你不想再作修改,就這樣)

+0

即使當我刪除其中一個 – Cemre

0

我的老習慣使我的實體中的身份字段的設置者受到保護,以保護外部的損壞。然而,在這種情況下,它導致身份字段被更新,這是問題的原因。

+0

你也應該刪除你的問題 - 沒有提及你模型和它的屬性,所以這對任何人都沒有用。 –

+0

@StephenMuecke我無法刪除它,因爲有答案...但我會編輯它 – Cemre