重定向到操作时传递参数

时间:2012-05-03 18:44:07

标签: asp.net-mvc

我想在重定向到动作时传递参数,然后将该参数绑定到模型。这是我到目前为止,有人可以告诉我如何做到这一点吗?

执行重定向的操作使用以下语句:

return RedirectToAction("TwinWithFacebook", new { id = facebookID }); 

然后我的意思是:

[HttpGet]
    public ActionResult TwinWithFacebook(long id)
    {
        //use viewdata to store id here?
        return View();
    }

我的帖子:

 [HttpPost]
    public ActionResult TwinWithFacebook(FacebookConnectModel fbc)
    {
        //assign this here?
        //fbc.facebookId = id;

3 个答案:

答案 0 :(得分:1)

您必须仅使用指定的id参数将模型提供给视图

public ActionResult TwinWithFacebook(long id)
{
     FacebookConnectModel fbc = new FacebookConnectModel(id);
     return View(fbc);
}

然后在您的视图中,您可以使用Html帮助器来放置这样的表单:

@model FacebookConnectModel
@Html.BeginForm()
{
    @Html.TextBoxFor(x => x.Name)
    @Html.HiddenFor(x => x.Id)
    <input type"submit" />
}

然后当您点击提交按钮时发布模型,正确且完全填充的模型将作为参数传递

答案 1 :(得分:0)

return RedirectToAction("TwinWithFacebook", new FacebookConnectModel(...){ or here ...} );

答案 2 :(得分:0)

当你进行GET时,你想要查找具有该id的对象,对吗?

public ActionResult TwinWithFacebook(long id)
{
    // Basically, use the id to retrieve the object here
    FacebookConnectModel fbc = new FacebookConnectModel(id);

    // then pass the object to the view.  Again, I'm assuming this view is based
    // on a FacebookConnectModel since that is the object you are using in the POST
    return View(fbc);
}