为什么DefaultModelBinder不绑定URL中的路由值ID

时间:2016-04-20 12:43:07

标签: asp.net-mvc post get model-binding

ASP.NET MVC

简而言之: 我有一个行动和一个后期行动

当我输入浏览器localhost:port / Employee / Edit / 1时我调用了get动作,所以在URL中我有这个整个url字符串。当我按下提交按钮时,在post动作中,defaultmodelbinder不会从URL绑定id !!!!我必须为id添加HIDDEN FIELD。但为什么?我也有删除动作(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 =&gt; 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>}

1 个答案:

答案 0 :(得分:0)

  

因为方法中的参数名为id且属性为   你的模型名为EmployeeId它们不一样。如果你   将模型属性更改为Id它将被绑定

谢谢,Stephen Muecke