【问题标题】:UpdateModel not updating my modelUpdateModel 没有更新我的模型
【发布时间】:2012-08-08 13:06:25
【问题描述】:

我一定是设置不正确,因为我无法让 UpdateModel 函数根据通过 FormCollection 传入的信息正确更新我的模型。

我的视图如下:

@model NSLM.Models.Person
@{
    ViewBag.Title = "MVC Example";
}
<h2>My MVC Model</h2>
<fieldset>
    <legend>Person</legend>
    @using(@Html.BeginForm())
    {
        <p>ID: @Html.TextBox("ID", Model.ID)</p>
        <p>Forename: @Html.TextBox("Forename", Model.Forename)</p>
        <p>Surname: @Html.TextBox("Surname", Model.Surname)</p>
        <input type="submit" value="Submit" />
    }  
</fieldset>

我的模型是:

    namespace NSLM.Models
    {
        public class Person
        {
            public int ID;
            public string Forename;
            public string Surname;
        }
    }

我的控制器是:

        [HttpPost]
        public ActionResult Details(FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here                
                Models.Person m = new Models.Person();

                // This doesn't work i.e. the model is not updated with the form values
                TryUpdateModel(m);

                // This does work 
                int.TryParse(Request.Form["ID"], out m.ID);
                m.Forename = Request.Form["Forename"];
                m.Surname = Request.Form["Surname"];

                return View(m);
            }
            catch
            {
                return View();
            }
        }

如您所见,如果我手动分配每个属性都可以正常工作,那么我没有设置什么可以让模型使用表单值进行更新?

谢谢,

标记

【问题讨论】:

    标签: asp.net-mvc razor updatemodel


    【解决方案1】:

    用模型中的属性替换字段,即:

    namespace NSLM.Models
    {
        public class Person
        {
            public int ID {get; set;}
            public string Forename {get; set;}
            public string Surname {get; set;}
        }
    }
    

    【讨论】:

    • 谢谢,这是我的问题。
    【解决方案2】:

    当调用到达 action 方法时,任何自动模型绑定都已执行。尝试更改您的操作方法的输入参数以接受 Person 实例。在这种情况下,模型绑定器将尝试创建实例并根据表单传递的值填充它。

    【讨论】:

    • 谢谢,我试过这个,但没有运气。如果我通过一个人实例,那么它的所有属性都是空的。
    【解决方案3】:

    试试这个:

    视图:

    @model NSLM.Models.Person
    @{
        ViewBag.Title = "MVC Example";
    }
    <h2>My MVC Model</h2>
    <fieldset>
        <legend>Person</legend>
        @using(@Html.BeginForm())
        {
             @Html.HiddenFor(model => model.ID)
    
            <p>Forename: @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </p>
            <p>Surname: @Html.EditorFor(model => model.Surname)
                @Html.ValidationMessageFor(model => model.Surname)
            </p>
            <input type="submit" value="Submit" />
        }  
    </fieldset>
    

    控制器:

    [HttpPost]
    public ActionResult Details(Person p)
    {
        if (ModelState.IsValid)
        {
            db.Entry(p).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(p);
    }
    

    【讨论】:

    • 谢谢,我刚刚尝试过,“p”对象具有所有空属性。在这个过程的早期,我一定有什么东西遗漏了,但我就是看不到它是什么。
    • 可能是因为无法根据文本框字段检索到id。我已经编辑了答案,在视图中添加了一个 HiddenFor 方法,该方法应该有助于将模型与 id 绑定。
    猜你喜欢
    • 2013-10-12
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 2020-04-30
    相关资源
    最近更新 更多