ASP.NET MVC
简而言之:我有一个get操作和一个post操作
当我在浏览器localhost:port/Employee/Edit/1中输入时,我调用了get action,因此在URL中我有完整的url字符串。当我按下提交按钮时,在post操作中,defaultmodelbinder没有绑定来自URL的id!我必须为id添加隐藏字段。但是为什么呢?我也有删除操作(post),它也获取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 => 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>}发布于 2016-04-20 20:53:53
因为方法中的参数名为id,而模型中的属性名为EmployeeId,所以它们并不相同。如果您将模型属性更改为Id,它将被绑定
谢谢,斯蒂芬·穆克
https://stackoverflow.com/questions/36744180
复制相似问题