0

I had asked question yesterday, which can be found here and based upon the answer I got from asp.net/mvc forum which can be found here, I was told to clear my modelstate, as by default my form tends to hold its default value, and not the value I just tried to update. So, I added Modelstate.Clear(), which still doesn't work. Can anyone tell me if i'm using the ModelState.Clear() in a wrong place or if I have to change something?

So, here is the problem, I have a edit form which shows its current values in textboxes, when user clicks edit button. If a user wants to edit some current value which is shown in textbox he edits the value in text box and clicks the save changes button. What currently is happening is in my HttpPost method when i check the values that are being passed, I don't get the new value user just provided, rather I get the value that was shown as current value in form. But when I check in the developer tools in chrome, it shows the new value user just provided as the value that is being passed to server.

Here is my view

@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>

Here is the get and post method in controller

        [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" });

        }
4

1 回答 1

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; }
    } 
}

您同时发回Propertiesand HiddenProperties,但仅更改Properties. modelbinder 设置新值,Properties然后设置依次设置的值HiddenPropertiesProperties您刚刚覆盖了您的更改。

我不确定你到底想做什么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" });
}

编辑 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表单域已被删除。

于 2013-07-24T14:32:43.763 回答