【问题标题】:edit form not working, data is being passed to server but not the httpPost method编辑表单不起作用,数据正在传递到服务器但不是 httpPost 方法
【发布时间】:2013-07-24 14:07:01
【问题描述】:

我昨天问过问题,可以在here 和 根据我从 here 的 asp.net/mvc 论坛获得的答案,我被告知要清除我的模型状态,因为默认情况下我的表单倾向于保持其默认值,而不是我刚刚尝试更新的值.所以,我加了Modelstate.Clear(),还是不行。谁能告诉我我是否在错误的地方使用了 ModelState.Clear() 或者我是否需要更改某些内容?

所以,问题来了,我有一个编辑表单,当用户单击编辑按钮时,它会在文本框中显示其当前值。如果用户想要编辑文本框中显示的某个当前值,他编辑文本框中的值并单击保存更改按钮。当前正在发生的事情是在我的 HttpPost 方法中,当我检查正在传递的值时,我没有得到用户刚刚提供的新值,而是得到了在表单中显示为当前值的值。 但是当我在 chrome 中签入开发人员工具时,它会显示刚刚提供的新值 user 作为正在传递给服务器的值。

这是我的看法

@using BootstrapSupport
@model AdminPortal.Areas.Hardware.Models.EditModule
@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/shared/_BootstrapLayout.basic.cshtml";
}

<fieldset>
    <legend>Module <small>Edit</small></legend>
     @using (Html.BeginForm("Edit", "Module"))
    {
        @Html.ValidationSummary(true)
        @Html.HiddenFor(m=>m.Id)
        for(var i = 0; i < Model.Properties.Count(); i++)
        {
            @Html.HiddenFor(model=>model.HiddenProperties[i].Name)
             @Html.HiddenFor(model=>model.HiddenProperties[i].Value)
            <label class="label">@Model.Properties[i].Name</label>
            <div class="input-block-level">@Html.TextBoxFor(model => model.Properties[i].Value)</div>
        }

         <div class="form-actions">
        <button type="submit" class="btn btn-primary" id="Submit">Save changes</button>
        @Html.ActionLink("Cancel", "ModuleList", null, new { @class = "btn " })
    </div>

    }
</fieldset>
<p>
    @Html.ActionLink("Back to List", "ModuleList")
</p>

这是控制器中的get和post方法

        [HttpGet]
        public ActionResult Edit(long id)
        {
            var module = _repository.GetModuleProperties(id);
            ModelState.Clear();
            return View(module);
        }

        [HttpPost]
        public ActionResult Edit(EditModule module)
        {
            ModelState.Clear();
            if (ModelState.IsValid)
            { 
                 _repository.SaveModuleEdits(module); 
                Information("Module was successfully edited!");
                return RedirectToAction("ModuleList", "Module", new {area = "Hardware"});
            }
            Error("Edit was unsuccessful, if the problem persists please contact Merijn!");
            return RedirectToAction("ModuleList", "Module", new { area = "Hardware" });

        }

【问题讨论】:

    标签: asp.net-mvc forms asp.net-mvc-4 view http-post


    【解决方案1】:

    问题出在你的模型上:

    public class EditModule
    {
        public long Id { get; set; }
        public List<PropertyViewModel> Properties { get; set; }
        public List<PropertyViewModel> HiddenProperties
        {
            get { return Properties; }
            set { Properties = value; }
        } 
    }
    

    您同时发回了Properties 和HiddenProperties,但只更改了Properties。 modelbinder 在Properties 中设置新值,然后为HiddenProperties 设置值,这反过来又设置Properties,而您刚刚覆盖了您的更改。

    我不确定你到底想用 HiddenProperties 做什么,但它目前的设置完全被破坏了。

    更新:建议的更改

    型号

    public class EditModule
    {
        public long Id { get; set; }
        public List<PropertyViewModel> Properties { get; set; }
    }
    

    删除了HiddenProperties 属性

    控制器动作

    [HttpPost]
    public ActionResult Edit(long id, EditModule module)
    {
        var originalModule = _repository.GetModuleProperties(id);
    
        // do whatever comparisons you want here with originalModule.Properties / module.Properties 
    
        if (ModelState.IsValid)
        { 
            _repository.SaveModuleEdits(module); 
            Information("Module was successfully edited!");
            return RedirectToAction("ModuleList", "Module", new {area = "Hardware"});
        }
        Error("Edit was unsuccessful, if the problem persists please contact Merijn!");
        return RedirectToAction("ModuleList", "Module", new { area = "Hardware" });
    }
    

    Edit POST 版本采用id,就像 GET 版本一样。您可以使用这个id 从数据库中查找模块的原始版本,然后您可以比较Properties 的原始版本和发布版本。

    查看

    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
        @Html.HiddenFor(m=>m.Id)
        for(var i = 0; i < Model.Properties.Count(); i++)
        {
            <label class="label">@Model.Properties[i].Name</label>
            <div class="input-block-level">@Html.TextBoxFor(model => model.Properties[i].Value)</div>
        }
    
        <div class="form-actions">
            <button type="submit" class="btn btn-primary" id="Submit">Save changes</button>
            @Html.ActionLink("Cancel", "ModuleList", null, new { @class = "btn " })
        </div>
    }
    

    Html.BeginForm() 语法告诉 Razor 只使用当前页面的 URL 作为表单的操作。 HiddenProperties 表单字段已被删除。

    【讨论】:

    • 我希望HiddenProperties 保存当前值,这样当用户“如果编辑”一个值时,它就会保存在Properties 中。提交表单后,我希望 HiddenProperties 具有旧值,Properties 具有新值,通过它我可以检查用户是否对当前值进行了一些更改。如果不是,我会抛出错误,说没有任何改变。所以 HiddenProperties 就像一个保存当前值的容器,通过它我可以检查用户是否进行了一些更改
    • 我需要对模型进行哪些更改才能达到预期的效果?
    • 我会检查一下,让你知道会发生什么
    猜你喜欢
    • 1970-01-01
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多