我有一個ASP.NET MVC表單,當提交時可以返回ActionResult
,如果數據有錯誤,或者一切正常,它會重定向到返回FileResult
的不同操作。ASP.NET MVC如何在操作返回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中的錯誤,以便用戶可以糾正並再試一次。
這一切工作正常,除了我希望表單被重置或清除,如果服務器返回一個文件。目前用戶獲得一個文件對話框,但表單保留了提交的值。如何在成功提交後重置表單?
請參閱http://stackoverflow.com/questions/2345756/ http://stackoverflow.com/questions/2297670/ http://stackoverflow.com/questions/707569/ – 2010-02-28 22:02:45