简体   繁体   English

调用控制器和操作方法并传递模型值

[英]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.

相关问题 如何使用ajax调用将模型传递给控制器​​动作? - How to pass model to controller action using ajax call? 没有模型绑定的控制器操作方法中如何使用文本框值? - How textbox values are available in controller action method without model binding? 如何从jquery调用值并将其传递给控制器​​方法 - How to call and pass values to a controller method from jquery 将模型值传递给控制器​​方法? - Pass model value to controller method? 如何在按下提交按钮时将整个模型对象从视图传递到jquery ajax调用的控制器动作? - How to pass whole model object from view to controller action from jquery ajax call on pressing submit button? 将数据表模型传递给MVC中的控制器操作 - Pass a datatable model to controller action in MVC 如何将模型值从Test类传递到Controller类中的操作方法 - How to pass Model value from Test class to Action Method in Controller class 选中复选框时,如何将当前 model 详细信息的值传递给 asp.net mvc 中的 controller 操作 - How to pass values of the current model details to controller action in asp.net mvc when checkbox is checked 当我们将上传Excel传递给控制器​​中的action方法时,如何在ajax调用中添加更多参数 - How to add more parameters in ajax call when we pass the upload Excel to action method in controller 如何将这两个参数传递给控制器​​的动作方法? - How to pass these 2 parameters to controller action method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM