2016-04-20 11 views
0

ASP.NET MVCWarum Default nicht Route Wert ID von URL binden

Kurz: Ich habe eine get Aktion und einen Beitrag Aktion

, wenn ich in Browser localhost eingeben: port/Angestellter/Edit/1 Ich rufe Aktion, also in URL habe ich diese ganze URL-Zeichenfolge. Wenn ich Submit-Button drücke, bindet defaultmodelibinder in der Post-Action ID von URL !!!! Ich muss verstecktes Feld für ID hinzufügen. Aber warum? Ich habe auch Aktion löschen (Post), die auch ID erhält, und ich muss nicht verstecktes Feld für ID hinzufügen. Warum?

Genauer gesagt:

Ich habe das Modell:

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

Und 2 Aktionen

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

Und 1 View (HIER Ich habe diese Zeile schreiben @ Html.HiddenFor (Modell => 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

Da die Parameter in der Methode 'id' und die Eigenschaft benannt ist in Ihrem Modell ist' genannt gebunden EmployeeId' Sie sind nicht das Gleiche. Und wenn Sie die Modelleigenschaft in 'Id' ändern, wird es gebunden –

+0

oh, mein Gott, danke. so dumm Fehler –

Antwort

0

Da der Parameter in der Methode id heißt und die Eigenschaft in Ihr Modell heißt EmployeeId Sie sind nicht das Gleiche. Und wenn Sie das Modell Eigenschaft Id ändern wird

Danke, Stephen Muecke

+0

Warum kopieren Sie Paste Nachricht von Stephen? – Dilip

+0

Ich weiß nicht, wie ich seine Antwort als richtig markieren soll –

Verwandte Themen