[英]call controller and action method and pass model values
I'm calling another controller and action method here 我在这里调用另一个控制器和动作方法
[HttpPost]
public ActionResult Index(LoginModel loginModel)
{
if (ModelState.IsValid)
{ some lines of code . bla bla bla
return RedirectToAction("indexaction","premiumcontroller");
}
}
Now, what happens is the indexaction of premiumcontroller is now executed. 现在,会发生什么是现在执行premiumcontroller的indexaction。
How can i pass the values of loginmodel (or the loginmodel object) to the premiumcontroller? 如何将loginmodel(或loginmodel对象)的值传递给premiumcontroller? i cant figure it out.
我想不出来。 Thanks.
谢谢。
I'm using asp.net mvc 3. 我正在使用asp.net mvc 3。
You could pass them as query string parameters: 您可以将它们作为查询字符串参数传递:
return RedirectToAction(
"index",
"premium",
new {
id = loginModel.Id,
username = loginModel.Username,
}
);
and inside the index
action of premium
controller: 并且在
premium
控制器的index
操作中:
public ActionResult Index(LoginModel loginModel)
{
...
}
Another possibility is to use TempData: 另一种可能是使用TempData:
[HttpPost]
public ActionResult Index(LoginModel loginModel)
{
if (ModelState.IsValid)
{
// some lines of code . bla bla bla
TempData["loginModel"] = loginModel;
return RedirectToAction("index", "premium");
}
...
}
and inside the index
action of premium
controller: 并且在
premium
控制器的index
操作中:
public ActionResult Index()
{
var loginModel = TempData["loginModel"] as LoginModel;
...
}
you can use new keyword to pass the values in the controller action... 你可以使用new关键字来传递控制器动作中的值...
return RedirectToAction(
"indexaction",
"premium",
new {
Id = loginModel.Id,
UserName = loginModel.UserName,
Password = loginModel.Password
}
);
in your other controller 在你的另一个控制器
public ActionResult indexaction(int id, string uName, string paswrd)
{
// do some logic...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.