我目前正在使用ViewModels绑定到我的所有CRUD操作,但有一些操作方法只返回部分视图:
public ActionResult Create(int parentId)
{
var viewModel = new MyCreateViewModel();
return PartialView("_Create", viewModel);
}
这些操作将通过AJAX从不同的视图(不同的实体)调用,并显示在jQuery对话框中。对话框按钮将通过POST
处理表单的$("#form").submit()
,另一个操作方法将处理表单,理想地重定向到调用部分视图的父视图:
[HttpPost]
public ActionResult Create(int parentId, MyCreateViewModel viewModel)
{
//Process the viewModel, map to EF models and persist to the database
return RedirectToAction(/*What should I insert here?*/);
}
由于我不知道哪个视图是POST
这个方法,我怎么知道应该将哪个视图重定向到?
答案 0 :(得分:1)
我会在viewModel中添加一个字符串属性,其中包含您要返回的视图的名称
[HttpPost]
public ActionResult Create(int parentId, MyCreateViewModel viewModel)
{
//Process the viewModel, map to EF models and persist to the database
return RedirectToAction(viewModel.ViewToRender);
}
答案 1 :(得分:1)
您可以在客户端重定向,而不是在操作方法中执行此操作。 在action方法中,您可以返回指示操作成功或失败的结果。在客户端,使用$ .ajax来处理结果
$('#form').submit(function () {
var self = $(this);
if (self.valid()) {
$.ajax({
type: "POST",
url: self.attr('action'),
data: self.serialize(),
success: function (data) {
if (data.Success == true) {
//redirect
} else{
//Error handling
}
},
error: function (ex) {
//Error handling
}
});
}
return false;
});