2016-04-20 42 views
0

ASP.NET MVC爲什麼DefaultModelBinder並不路線值ID從URL綁定

簡而言之: 我有一個獲取動作和動作後

,當我在瀏覽器中鍵入localhost:端口/員工/編輯/ 1我打電話獲取行動,所以在URL中我有這整個網址字符串。當我按提交按鈕,在後動作defaultmodelbinder不綁定ID從URL!我必須爲id添加隱藏字段。但爲什麼?我也有刪除行爲(後),也獲得ID,我不需要添加隱藏的字段爲ID。爲什麼?

更具體地說:

我有模式:

public class EmployeeViewModel 
{ 
    public Int32 EmployeeId { get; set; } 

    public String Name { get; set; } 

    public String Phone { get; set; } 

    public String Email { get; set; } 

    public String Other { get; set; } 
} 

而且2行動

public ActionResult Edit(int id) 
    { 
     try 
     { 
      EmployeeViewModel model; 
      using (var dbSession = NHibernateHelper.OpenSession()) 
      { 
       var employee = dbSession.Query<Employee>().First(e => e.EmployeeId == id && e.ExpireDate==null); 
       model = new EmployeeViewModel(employee); 
      } 
      return View(model); 
     } 
     catch 
     { 
      return View("Error"); 
     } 
    } 


    [HttpPost] 
    public ActionResult Edit(EmployeeViewModel model) 
    { 
     try 
     { 
      using (var dbSession=NHibernateHelper.OpenSession()) 
      using (var transaction=dbSession.BeginTransaction()) 
      { 
       var employee = model.ToEmployee(); 
       dbSession.Merge(employee); 
       transaction.Commit(); 
      } 

      return RedirectToAction("Index"); 
     } 
     catch 
     { 
      return View("Error"); 
     } 
    } 

和1個景(這裏我在此線@ Html.HiddenFor(模型=> model.EmployeeId))

@using (Html.BeginForm()){ 
@Html.AntiForgeryToken() 

<div class="form-horizontal"> 
    <hr /> 
    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
    <div class="form-group"> 
     @Html.HiddenFor(model => model.EmployeeId) 
     @Html.LabelFor(model => model.Name, htmlAttributes: new {@class = "control-label col-md-2"}) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Name, new {htmlAttributes = new {@class = "form-control"}}) 
      @Html.ValidationMessageFor(model => model.Name, "", new {@class = "text-danger"}) 
     </div> 
    </div> 

    <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.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 class="form-group"> 
     @Html.LabelFor(model => model.Other, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Other, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Other, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" value="Сохранить" class="btn btn-default" /> 
     </div> 
    </div> 
</div>} 
+0

由於該方法的參數被命名爲'id'和模型中的屬性名爲'EmployeeId'他們不一樣。如果您將模型屬性更改爲'Id',那麼將會綁定 –

+0

哦,天啊,謝謝。如此愚蠢的錯誤 –

回答

0

由於該方法中的參數被命名爲id,而屬性爲 ,因此您的模型被命名爲EmployeeId,它們並不相同。如果你 模型屬性更改爲標識將被綁定

謝謝,斯蒂芬·馬克

+0

爲什麼你從Stephen複製粘貼消息? – Dilip

+0

我不知道如何標記他的答案是正確的 –