首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么DefaultModelBinder不从URL绑定路由值ID

为什么DefaultModelBinder不从URL绑定路由值ID
EN

Stack Overflow用户
提问于 2016-04-20 20:43:07
回答 1查看 143关注 0票数 0

ASP.NET MVC

简而言之:我有一个get操作和一个post操作

当我在浏览器localhost:port/Employee/Edit/1中输入时,我调用了get action,因此在URL中我有完整的url字符串。当我按下提交按钮时,在post操作中,defaultmodelbinder没有绑定来自URL的id!我必须为id添加隐藏字段。但是为什么呢?我也有删除操作(post),它也获取id,我不需要为id添加隐藏字段。为什么?

更确切地说:

我有一个模型:

代码语言:javascript
复制
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个动作

代码语言:javascript
复制
 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) )

代码语言:javascript
复制
@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>}
EN

回答 1

Stack Overflow用户

发布于 2016-04-20 20:53:53

因为方法中的参数名为id,而模型中的属性名为EmployeeId,所以它们并不相同。如果您将模型属性更改为Id,它将被绑定

谢谢,斯蒂芬·穆克

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36744180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档