0

I want to pass a parameter when redirecting to an action, and then bind that parameter to the model. Here is what I have so far, can anybody tell me how to do this?

The action that is doing the redirect uses this statement :

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

Then my get is :

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

And my post :

 [HttpPost]
    public ActionResult TwinWithFacebook(FacebookConnectModel fbc)
    {
        //assign this here?
        //fbc.facebookId = id;
user517406
  • 13,623
  • 29
  • 80
  • 120

3 Answers3

1

You have to give the model to your view with only the id parameter assigned so

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

Then in your view you can use the Html helper to put a form like this:

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

and then when you hit the submit button you post the model, and the correct and fully filled model will be passed as parameter

Sloth
  • 172
  • 8
  • Would your method be the get or post? – user517406 May 04 '12 at 19:51
  • For some reason it would not pick up the id, so I had to get the id through this code instead FacebookConnectModel fbc = new FacebookConnectModel(Convert.ToInt64(Url.RequestContext.RouteData.Values["id"])); – user517406 May 04 '12 at 20:45
  • That can happen when the id is used as route parameter, and model parameter also (just the same name, 'id') then it sometimes happens with name conventions that the wrong variable is set, you can avoid this by using facebookId or something like that – Sloth May 07 '12 at 09:55
0
return RedirectToAction("TwinWithFacebook", new FacebookConnectModel(...){ or here ...} );
Nastya Kholodova
  • 1,301
  • 9
  • 18
0

When you do the GET, you want to look up an object with that id, correct?

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);
}
Andrew Aitken
  • 338
  • 3
  • 6
  • No, I don't want to lookup an object with the id, I want to use the id along with other details the user may input on the page I redirect to. – user517406 May 03 '12 at 19:02