上传完成后,如何将页面重定向到其他操作?到目前为止,status.html(xhr.responseText);
会在上传完成后在“创建”视图中创建索引的“局部”视图。到目前为止,“创建”操作返回一个JsonResult,其中包含我希望页面重定向到的URL。
<script>
$.noConflict();
jQuery(document).ready(function ($) {
var bar = $('.progress-bar');
var percent = $('.percent');
var status = $('#status');
$('#Myform').ajaxForm({
type: "POST",
contentType: "application/json; charset=utf-8",
beforeSend: function () {
status.empty();
var percentVal = '0%';
bar.width(percentVal);
percent.html(percentVal);
},
uploadProgress: function (event, position, total, percentComplete) {
var percentVal = percentComplete + '%';
bar.width(percentVal);
percent.html(percentVal);
},
complete: function (xhr) {
window.location.href = xhr.url;
}
});
});
</script>
创建动作
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(CreateVideoViewModel model)
{
if (ModelState.IsValid)
{
Video video = new Video();
if (model.File != null)
{
// ....
if (videoUpload != null && thumbnailUpload != null)
{
return Json(new { url = Url.Action("Index", new { projectId = model.ProjectId }) });
}
return View("Error");
}
return View("Error");
}
return View(model);
}
索引操作
public ActionResult Index(string projectId)
{
IndexVideoViewModel model = new IndexVideoViewModel
{
Project = db.Projects.Where(project => project.Id == projectId).First()
};
return View(model);
}