I’ve created a very simple MVC4Beta Application. It uses the generated project structure to display a simple form with a label, a text box and a submit button. The strange problem here is that the view gets not updated correctly after submitting the form
using Ajax.
The form is implemented in a partial view called “NameView.cshtml” and included a <div/> tag with the id=“NameViewContainer”. I use Ajax.BeginForm() to submit the form and set the UdpdateTargetId = “NameViewContainer”.
The form has a textbox and its value is set to ViewData[“Name”]. ViewData[“Name”] is initialized to “Peter” when the Index() action of the HomeController is called.
The Action called on submit sets ViewData[“Name”] = “John” and returns PartialView(“NameView”). After submitting the form the textbox still displays “Peter”? Why? I was expecting “John”?
You need to remove the Name from the ModelState if you want to see the changes in your view:
public ActionResult SaveName(string Name)
{ // ignore any entered values and reset Name to "John"... ModelState.Remove("Name");
this.ViewData["Name"] = "John";
return PartialView("NameView");
}
The assumption in MVC is that if you are returning back to the original form it's because there was a validation failure. If the user entered the value 'Peter' in the textbox, you may want the value to still be there when you show an error message next to
it.
But I must confess that this assumption doesn’t make sense to me as long as there are no broken validation rules (specified by attributes) and no errors have been added explicitly to the ModelState....
AndreasKuhlm...
Member
1 Points
4 Posts
PartialView not updated to current values after ajax post (MVC4Beta)
May 04, 2012 04:43 PM|LINK
Hi,
I’ve created a very simple MVC4Beta Application. It uses the generated project structure to display a simple form with a label, a text box and a submit button. The strange problem here is that the view gets not updated correctly after submitting the form using Ajax.
The form is implemented in a partial view called “NameView.cshtml” and included a <div/> tag with the id=“NameViewContainer”. I use Ajax.BeginForm() to submit the form and set the UdpdateTargetId = “NameViewContainer”.
The form has a textbox and its value is set to ViewData[“Name”]. ViewData[“Name”] is initialized to “Peter” when the Index() action of the HomeController is called.
The Action called on submit sets ViewData[“Name”] = “John” and returns PartialView(“NameView”). After submitting the form the textbox still displays “Peter”? Why? I was expecting “John”?
Here is the code:
Index.cshtml:
@{
ViewBag.Title = "Home Page";
}
@{ Html.RenderPartial("NameView");}
NameView.cshtml:
@using (Ajax.BeginForm("SaveName", "Home", new AjaxOptions(){{@Html.Label("Name")@Html.TextBox("Name", ViewData["Name"])Code added to HomeController.cs:
[OutputCache(Duration = 0, VaryByParam = "*", Location = OutputCacheLocation.None, NoStore = true)
public calls HomeControler
{
….
public ActionResult Index()
{
ViewData["Name"] = "Peter";
return View();
}
{return PartialView("NameView");….
}
Script added to _Layout.cshtml:
<script type="text/javascript">
$.ajaxSetup({
cache: false
});
</script>
Andreas
mm10
Contributor
6429 Points
1184 Posts
Re: PartialView not updated to current values after ajax post (MVC4Beta)
May 04, 2012 05:10 PM|LINK
You need to remove the Name from the ModelState if you want to see the changes in your view:
public ActionResult SaveName(string Name)
{ // ignore any entered values and reset Name to "John"...
ModelState.Remove("Name");
this.ViewData["Name"] = "John";
return PartialView("NameView");
}
The assumption in MVC is that if you are returning back to the original form it's because there was a validation failure. If the user entered the value 'Peter' in the textbox, you may want the value to still be there when you show an error message next to it.
AndreasKuhlm...
Member
1 Points
4 Posts
Re: PartialView not updated to current values after ajax post (MVC4Beta)
May 04, 2012 06:01 PM|LINK
Thanks, it works! I use ModelState.Clear().
But I must confess that this assumption doesn’t make sense to me as long as there are no broken validation rules (specified by attributes) and no errors have been added explicitly to the ModelState....
Andreas