【发布时间】:2011-01-22 02:38:48
【问题描述】:
我有一个 ASP.NET MVC 表单,如果数据有错误,或者如果一切正常,它在提交时可以返回 ActionResult,它会重定向到返回 FileResult 的不同操作。
我创建了一些示例,以提供我在做什么的想法。这是html:
<%
using (Html.BeginForm())
%>
<%= Html.ValidationSummary(
"Edit was unsuccessful. Please correct the errors and try again.") %>
<%= Html.TextBox("YourName", Model.YourName)%>
<%= Html.ValidationMessage("YourName", "*") %>
<input type="submit" />
<% } %>
控制器代码为:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult MyPostSample(string YourName)
{
if (string.IsNullOrEmpty(YourName))
{
ModelState.AddModelError("YourName", "You must specify a name");
}
if (ModelState.IsValid)
{
// get the file
RedirectToAction("GetFile");
}
else
{
// return this actions View with errors
return View();
}
}
public FileResult GetFile(string YourName)
{
// return file here
}
ActionResult 的原因是填充 ModelState 错误,以便用户可以更正并重试。
这一切都很好,除了我希望在服务器返回文件时重置或清除表单。目前,用户获得了一个文件对话框,但表单保留了提交的值。提交成功后如何重置表单?
【问题讨论】:
标签: html asp.net-mvc forms submit