Roh*_*hit 5 asp.net-mvc action controller asp.net-mvc-4
我想将一个类对象从一个控制器操作传递到不同控制器的操作。
发件人操作
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 using TempData. 如果没有,那么我只会使用 TempData。
查找
如果我传递一些简单的数据,例如new {id = courseViewModel.CourseDuration}并将接收器操作中的参数更改为 id,则 id 会被正确接收。
类似问题
问题1
问题2
问题3
问题4
问题5
问题6,尝试使用这个但没有锻炼
问题7
问题8
问题9
问题10
上述问题的大多数答案都可以追溯到 2008/09 年并使用 tempdata。
用这个
return RedirectToAction("ActionName", "ControllerName", modelObj);
Run Code Online (Sandbox Code Playgroud)
在你的情况下
return RedirectToAction("CreateNewProject", "Course", courseViewModel);
Run Code Online (Sandbox Code Playgroud)
您还可以使用
TempData
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15680 次 |
| 最近记录: |