This is the model example.cs
namespace View_Partial_Editor.Models
{
public class ExampleView
{
...
public string Field1 { get; set; }
public string Field2 { get; set; }
public string Field3 { get; set; }
public string Field4 { get; set; }
public string Field5 { get; set; }
...
}
}
I have this view example.cshtml:
@model View_Partial_Editor.Models.ExampleView
@{Html.RenderPartial("EditExample",Model);}
@Html.TextBoxFor(m => m.Field1)
Then i have this partialView EditExample.cshtml:
@model View_Partial_Editor.Models.ExampleView
@Html.HiddenFor(m => m.Field1)
@using (Ajax.BeginForm("EditExample", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "partial" }))
{
<div>
@Html.EditorFor(m => m, "Editor", null)
</div>
<p>
<input id="buttona" type="submit" value="Save" />
</p>
}
I have this controller ExampleController:
namespace View_Partial_Editor.Controllers
{
public class ExampleController : Controller
{
//
// GET: /Example/
public ActionResult Example()
{
return View();
}
[HttpPost]
public ActionResult EditExample(ExampleView example)
{
example.Field1 ="7";
return View("Example", example);
}
}
}
And this is the editor that is called in the partial editor.cshtml
@model View_Partial_Editor.Models.ExampleView
<div class="editor-label">
@Html.LabelFor(m => m.Field1 )
</div>
<div class="editor-field">
@Html.EditorFor(m => m.Field1)
@Html.ValidationMessageFor(m => m.Field1)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Field2)
</div>
<div class="editor-field">
@Html.EditorFor(m => m.Field2)
@Html.ValidationMessageFor(m => m.Field2)
</div>
My problem is that i want to modify the data of the model in the controller in the ajax call, and return the model modified to the exampleView.But when the ajax called finish the value that i change in the controller is not changed in the model
Edit: The thing that i want is to send the call to the ajax methos, save something in the database, then modify the model, and in the example view i want to have that model with the changes.
In this moment, if i replace the partial view with the result of the ajax, the model in the example view is not modified.Another way is to replace the full example view, so the model is received there, but i have to pass a lot of fields using Html.HiddenFor, is possible to make this without replacing the views only returnng the model with the changes
Try calling
ModelState.Clear()in your HttpPost action method. Html helpers use the values in the ModelState first, then the Model. If you change a value in the model on a post therefore, you need to clear the value from the ModelState.