我的页面有2个输入类型=文字..
@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"/>
}
在我的控制器中我这样做:
[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);
}
所以问题..当我的模型有效时,我将结果设置为“Success”,此时vm.TalkToUsRequest为null ..但是当呈现页面时,所有字段都具有与我提交时相同的值..甚至我设置vm.TalkToUsRequest = null !! 我该如何清除这些字段?
答案 0 :(得分:60)
因此,在这种情况下,如果返回到同一视图,则必须清除模型状态。 请尝试以下方法:
ModelState.Clear();
return View(vm);
}
答案 1 :(得分:2)
你的回答:
>>> import timeit
>>> dir(timeit)
['Timer', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__sp
ec__', '_globals', 'default_number', 'default_repeat', 'default_timer', 'dummy_src_name', 'gc', 'itertools', 'main', 're
indent', 'repeat', 'sys', 'template', 'time', 'timeit']
>>> help(timeit.timeit)
Help on function timeit in module timeit:
timeit(stmt='pass', setup='pass', timer=<built-in function perf_counter>, number=1000000, globals=None)
Convenience function to create Timer object and call timeit method.
>>> timeit.timeit('''a=""; a+="b"*1000;''')
0.14870357161726633
>>> timeit.timeit('''a=""; a+="b"*1000;''')
0.13576636550958954
>>>
>>>
>>> def testme():
... a = ""
... a = "eyang" * 10000
...
>>> timeit("testme()")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
>>>
>>> timeit("testme()", setup="from __main__ import testme")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
>>>
它将更新您的视图状态
如果您还想同时清除所有Modelstate.error 也用:
TryUpdateModel(yourmodelname);