简体   繁体   English

如何将 RedirectToAction 中的单个参数传递给 ASP .NET MVC 中的不同 controller?

[英]How to pass single parameter in RedirectToAction to different controller in ASP .NET MVC?

I know, a similar question is asked before.我知道,之前有人问过类似的问题。 But I couldn't find exact similar case.但我找不到完全相同的案例。 I want to pass model object in return RedirectToAction() to method in another controller.我想将 model object 作为回报 RedirectToAction() 传递给另一个 controller 中的方法。 My code:我的代码:

  MyModel mdl_obj=new MyModel();
  return RedirectToAction("mthdInAnotherController", "AnotherControllerName",new { mdl_obj = mdl_obj });

I am assigning some values to model object and sending it to method in another controller.我将一些值分配给 model object 并将其发送到另一个 controller 中的方法。 But that method has more than one arguments and I am not passing those.但是那个方法有不止一个 arguments 我没有通过那些。 While passing, model object is not null but in another controller, I am getting null value for model object. While passing, model object is not null but in another controller, I am getting null value for model object. What could be the reason?可能是什么原因?

TempData is for this scenario. TempData 适用于这种情况。 TempData is used to transfer data from view to controller, controller to view, or from one action method to another action method of the same or a different controller. TempData 用于将数据从视图传输到 controller、controller 到视图,或从一种操作方法传输到相同或不同 controller 的另一种操作方法。

The usage is用法是

    public ActionResult Foo()
    {
        // Store data into the TempData that will be available during a single redirect
        TempData["Model"] = new MyModel();
    
        // If you store something into TempData and redirect to a controller action that will be available to consume the data
        return RedirectToAction("bar");
    }
    
    public ActionResult Bar()
    {
        var mdl_obj = TempData["Model"];
        ...
    }

Reference: ViewBag, ViewData and TempData参考: ViewBag、ViewData 和 TempData

You will first need to check that the model class is declared within your HTML view like this:您首先需要检查 model class 是否在您的 HTML 视图中声明,如下所示:

@model MyModel

Without it the view cannot bind data from the controller.没有它,视图将无法绑定来自 controller 的数据。

The model class declared in your view must match the class returned by the controller method:视图中声明的 model class 必须与 Z594C103F2C6E04C0CAZAB059F03E 方法返回的 class 匹配:

[HttpGet]
public ActionResult mthdInAnotherController(string param1, string param1, ..., paramN)
{
    MyModel = new MyModel();
    // populate model here
    ...
    ...
    return View(myModel);
}

Also check the controller method called has matching input parameter names from your RedirectToAction() parameter object:还要检查调用的 controller 方法是否具有与 RedirectToAction() 参数 object 匹配的输入参数名称:

return RedirectToAction("mthdInAnotherController", "AnotherControllerName", 
    new { 
        param1 = val1, param2 = val2, ... ,paramN = valN 
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM