本文介绍了成功后清除字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个页面,其中有2个输入type = text.
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);
}
所以是问题..当我的模型有效时,我将结果设置为成功",此时vm.TalkToUsRequest为null ..但是,呈现页面时,所有字段的值都与提交时的值相同..即使我设置vm.TalkToUsRequest = null!如何清除此字段?
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:
ModelState.Clear();
return View(vm);
}
这篇关于成功后清除字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!