I have a page with 2 input type=text..
@model MVC3.ViewModel.TalkToUsVM
@using (Html.BeginForm())
{
<ul>
<li>@Html.TextBoxFor(m => m.TalkToUsRequest.Name)</li>
<li>@Html.TextBoxFor(m => m.TalkToUsRequest.Email)</li>
</ul>
<input type="submit" value="Save"/>
}
in my controller I do this:
[HttpPost]
public ActionResult Create(TalkToUsRequest talkToUsRequest)
{
var vm = new TalkToUsVM();
if (TryValidateModel(talkToUsRequest))
{
vm.Result = "Success";
return View("Create",vm);
}
vm = new TalkToUsVM
{
Result = "Errrooooooor",
TalkToUsRequest = talkToUsRequest
};
return View(vm);
}
so the problem.. when my model is valid, I set the result to “Success” and in this point vm.TalkToUsRequest is null.. but when page is rendered, all fields are with the same value that when I submited.. even I setting vm.TalkToUsRequest = null!!
How can I clear this fields?
So in this scenario you have to clear your model state if you return back to the same view.
Try following: