Editor templates/BeginForm does not update the val

2019-08-14 19:19发布

问题:

 @using (Ajax.BeginForm("SaveItemAndProperties", "HomeBuilder",
        new AjaxOptions
        {
            UpdateTargetId = "divSaveItemAndProps",
            InsertionMode = InsertionMode.Replace               
        }))
    {
        @Html.EditorForModel()        
        <input type="submit" value="Submit" />            
    }

In Model which is called from EditorForModel

@Html.EditorFor(m => m.PropertyValues)

PropertyValues is a list of properties and is a calling a EditorTemplate.

From the Action I change the value and then try to update the data back to the View

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public PartialViewResult SaveItemAndProperties(PropertyBuilderViewModel modelValues)
{
    //Change on property in modelValues
    return PartialView("PropertyBuilderControl", modelmodelValues);
}

When i am debugging i see the data propertly but it does not display in the view. Any idea why it is doing so.

回答1:

What are you changing in your action? HTML helpers such as TextBoxFor, HiddenFor, DropDownListFor, CheckBoxFor, ... first look at ModelState when binding and after that in the model. So if in your controller action you intend to do something like this:

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public PartialViewResult SaveItemAndProperties(PropertyBuilderViewModel modelValues)
{
    modelValues.Foo = "some new value";
    return PartialView("PropertyBuilderControl", modelmodelValues);
}

make sure you remove that value from the model state or you won't see any updates once you render the view again:

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public PartialViewResult SaveItemAndProperties(PropertyBuilderViewModel modelValues)
{
    ModelState.Remove("Foo");
    modelValues.Foo = "some new value";
    return PartialView("PropertyBuilderControl", modelmodelValues);
}