0

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

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

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;
4

3 回答 3

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" />
}

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

于 2012-05-03T21:17:06.540 回答
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);
}
于 2012-05-03T18:49:32.110 回答
0
return RedirectToAction("TwinWithFacebook", new FacebookConnectModel(...){ or here ...} );
于 2012-05-03T18:46:40.017 回答