如果我的ModelState中存在错误而不丢失我的ModelState信息,如何返回不同操作的结果或将用户移动到其他操作?
情景是; 删除操作接受由我的索引操作/视图呈现的DELETE表单中的POST.如果删除中出现错误,我想将用户移回索引操作/视图,并显示删除操作存储的错误ViewData.ModelState.如何在ASP.NET MVC中完成?
[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Delete)]
public ActionResult Delete([ModelBinder(typeof(RdfUriBinder))] RdfUri graphUri)
{
if (!ModelState.IsValid)
return Index(); //this needs to be replaced with something that works :)
return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud) 我想将一个类对象从一个控制器操作传递到不同控制器的操作。
发件人操作
public class CourseController : Controller
{
[HttpPost]
public ActionResult CreateNewCourse(CourseViewModelBase courseViewModel)
{
if (ModelState.IsValid)
{
// Do some stuff
return RedirectToAction("CreateNewProject", "Project",
new { courseVM = courseViewModel});
}
// Bad happened
return View("CreateNewCourse", courseViewModel);
}
Run Code Online (Sandbox Code Playgroud)
接收者行动
public class ProjectController : Controller
{
[HttpGet]
public ActionResult CreateNewProject(CourseViewModelBase courseVM)
{
// Use CourseVM data and do other stuff
return View("Create", projectCreateViewModel);
}
}
Run Code Online (Sandbox Code Playgroud)
我在发送方操作中正确获取数据,并且从重定向到操作调用正确调用接收方操作。但是Receiver Action中的courseVM为null。
我知道这是一个非常古老的问题,并且已经被重复问过。但我发现大多数答案都建议使用TempData并在2008/2009年回答过。我相信会有某种方式使用RedirectToAction without …