我初學MVC3和仍然在學習。我試着寫一個應用程序(MVC3剃刀),它允許用戶選擇文件並上傳/保存。在上傳/保存過程中,我只想將「等待」文本顯示爲部分視圖。由於局部視圖只要Web應用程序已啓動,我從HomeController中遇到錯誤加載我有問題 - [HTtpPost] wait方法,因爲它不能跟蹤列表文件中對象的工作。當然,上傳後會填寫文件列表。我不知道如何解決這個問題,需要你的幫助。先謝謝你。MVC3剃刀:如何讓有條件加載的局部視圖?
我HomeController.cs:
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult UploadFile(IEnumerable<HttpPostedFileBase> attachments)
{
foreach (var file in attachments)
{
// do something
}
return RedirectToAction("Wait");
}
public ActionResult Wait()
{
// do something
ViewBag.Message = "Wait...";
return View();
}
[HttpPost]
public ActionResult Wait(FormCollection formCollection)
{
Work job = MvcApplication.GetWork();
if (job.Files.Any())
{
return RedirectToAction("SubmitWork");
}
else
{
return View();
}
}
視圖Index.cshtml:
@{
ViewBag.Title = "FirstTry";
}
<p>
<div id="AddFiles">
@Html.Partial("_AddFiles")
</div>
</p>
<div id ="Wait">
@Html.Partial("_Wait")
</div>
局部視圖_Wait.cshtml:
@{
ViewBag.Title = "Wait...";
}
@ViewBag.Message
@using (Html.BeginForm("Wait", "Home", FormMethod.Post, new
{
id = "waitform"
}))
{
}
<script type="text/javascript">
window.setTimeout("document.getElementById('waitform').submit()", 1000);
</script>
局部視圖_AddFiles.cshtml:
@using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new{id = "uploadForm", enctype = "multipart/form-data"}))
{
@(Html.Telerik().Upload().Name("attachments").Multiple(true)
.Async(async => async.AutoUpload(true))
)
<input type="submit" value="Send" class="t-button" />
<input type="reset" value="Reset" class="t-button" />
}