Javascript ASP.NET MVC3:模式对话框中的编辑视图
我们正在编写一个ASP.NET MVC3应用程序,并希望它“丰富”,例如使用引导和模式对话框 我现在想知道如何实现模态对话框,而不破坏ASP.NET(ModelErrors,…)中所有优秀的人员 工作流程应如下所示:Javascript ASP.NET MVC3:模式对话框中的编辑视图,javascript,jquery,asp.net,asp.net-mvc-3,razor,Javascript,Jquery,Asp.net,Asp.net Mvc 3,Razor,我们正在编写一个ASP.NET MVC3应用程序,并希望它“丰富”,例如使用引导和模式对话框 我现在想知道如何实现模态对话框,而不破坏ASP.NET(ModelErrors,…)中所有优秀的人员 工作流程应如下所示: IndexView带有一个项目列表,每个项目都有一个显示模式对话框的actionlink @Ajax.ActionLink( "Edit", // Link Text "Edit", // ActionMethod new
@Ajax.ActionLink(
"Edit", // Link Text
"Edit", // ActionMethod
new { id = item.Id }, // RouteValues
new AjaxOptions {
HttpMethod = "Get",
OnBegin = "modal.showModalDiv()",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "modal-div",
OnSuccess = "modal.ajaxSuccess()" },
new { data-toggle = "edit-modal" } // HTML-Attributes
)
@{
AjaxOptions ajaxOptions = new AjaxOptions() {
HttpMethod = "Post", OnSuccess="modal.hideModalDiv()"
};
}
@using (Ajax.BeginForm("Edit"), ajaxOptions){
... element to edit item ...
<input type="submit" value="submit" />
}
[HttpPost]
public ActionResult Edit(Guid id, ItemModel model)
{
if(model != null && ModelState.IsValid)
{
return RedirectToAction("Index")
}
else
{
return PartialView(model);
}
}
我想你已经解决了这个问题或者找到了解决方法,但是如果你没有,我怀疑如果你返回一个包含javascript的局部视图来进行重定向,你可以得到你描述的行为 例如: 重定向到index.cshtml
@{ Layout = null; }
<script type="text/javascript">
window.location.href = "@Url.Action("Index")";
</script>
不是最优雅的,但应该有效,我说应该,因为我没有测试过这个…嗨,谢谢你的回答。我想你没有理解我的问题。你的建议看起来(除了ModelState.IsValid)和我的完全一样。我的问题是,如何处理不同类型的响应(PartialViewResult替换div与Redirectresult重定向或替换另一个div)?
[HttpPost]
public ActionResult Edit(Guid id, ItemModel model)
{
if(model != null && ModelState.IsValid)
{
return RedirectToAction("Index")
}
else
{
return PartialView(model);
}
}
@{ Layout = null; }
<script type="text/javascript">
window.location.href = "@Url.Action("Index")";
</script>
[HttpPost]
public PartialViewResult Edit(Guid id, ItemModel model) {
try{
//Save Item ...
return PartialView("RedirectToIndex")
} catch (Exception ex){
ModelState.AddModelError("", "An error occured");
return PartialView(model);
}
}