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?
To clear an input field after submitting:Add a click event listener to a button. When the button is clicked, set the input field's value to an empty string. Setting the field's value to an empty string resets the input.
The reset() method resets the values of all elements in a form (same as clicking the Reset button). Tip: Use the submit() method to submit the form.
So in this scenario you have to clear your model state if you return back to the same view. Try following:
ModelState.Clear(); return View(vm); }
Your answer :
TryUpdateModel(yourmodelname);
and it will update your view state
and if you also want to clear all Modelstate.error at the same time you can also use :
ModelState.Clear();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With