【问题标题】:How to pass model data to one controller to another controller如何将模型数据传递到一个控制器到另一个控制器
【发布时间】:2015-03-02 16:16:46
【问题描述】:

可以将模型数据传递到一个控制器到另一个控制器吗?

我想将模型数据传递给一个控制器或另一个控制器。

[HttpPost]
        public ActionResult Personal(StudentModel student)
        {                          
                return RedirectToAction("nextStep", new { model = student});          
        }

        public ActionResult nextStep(StudentModel model)
        {           
            return View(model);
        }

nextStep 控制器型号值为空。 怎么做? 我需要在 nextStep conreoller 中对 StudentModel 数据进行处理。

【问题讨论】:

标签: c# asp.net-mvc


【解决方案1】:

您正在使用RedirectToAction。它将发出 GET 请求。您可以通过两种方式在此处传递模型。

1.临时数据

您需要将模型保存在 TempData 中并生成 RedirectToAction。但是,限制是它仅适用于即时请求。在你的情况下,这不是问题。你可以用TempData来做到这一点

public ActionResult Personal(StudentModel student)
{                          
       TempData["student"] = student;
       return RedirectToAction("nextStep", "ControllerName");          
}

public ActionResult nextStep()
{      
       StudentModel model= (StudentModel) TempData["student"];
       return View(model);
}

2。传入查询字符串

由于请求是 GET,我们可以将数据作为带有模型属性名称的查询字符串传递。 MVC 模型绑定器将解析查询字符串并将其转换为模型。

 return RedirectToAction("nextStep", new { Name = model.Name, Age=model.Age });

还要注意在查询字符串中传递合理的数据是不可取的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    相关资源
    最近更新 更多