1
votes

Perhaps I'm making this harder than it needs to be, but...

I have a strongly-typed view (a registration page) which works great however users arrive at the view using a link which is sent to them via email and this URL has a few querystring parameters in it that I need to work with when the registration form is submitted.

I'm able to capture these parameters in the controller when the page is loaded and stuff them in the "view bag" and such, but I can't figure out how to hand them back to the controller when the form is submitted. The only parameter to the controller method is an instance of the "type", and I've even tried extending this type to hold the additional parameters but when I try to assign them in the view, there is no instance of the object (I'm guessing this gets instantiated during the POST which would explain why it's not available when the page is rendered).

In a nutshell, I'm looking for a way to hold on to some values from the querystring and to access these values in the controller during the submission of a strongly-typed view, if this is even possible.

5
The point is that the query string is encoded in the URL. If you don't navigate away from the URL (e.g. by redirecting or setting the URL in JS) the browser will "stay put" on that URL. So to "keep" the query string parameters until you post on the same page (URL) you don't have to do anything special! There are already a lot of good answers on how to access those query string parameters and that works equally for the initial GET and the following POST actions as well.Tz_

5 Answers

3
votes

Add all the parameters you need to a hidden input tag inside a form. This will make those values get POSTed back to a controller action.

2
votes

Your URL with the parameters links to a GET Action, right? If so, add the parameters names as varaibles to the declaration of that Action. So for example, say my URL sent via email was:

http://mywebsite.com/register?id=511&sl=department

Then my corresponding Action is:

public ActionResult Register(int id, string sl)
{
    MyModel myModel = new MyModel();
    myModel.id = id;
    myModel.sl = sl;
    return View(myModel);
}

To keep those during a multi-step process you can either use Html.HiddenFor() in the view to add a hidden field or save to some other location (ie database).


If you don't want to add them to your model then you can do this:

public ActionResult Register(int id, string sl)
{
    ViewData["id"] = id;
    ViewData["sl"] = sl;
    return View();
}

And now in the view have a hidden field for each. Then in the POST-to controller action:

[HttpPost]
public ActionResult Register(MyModel myModel, int id, string sl)
{
    // the hidden fields are now in id and sl
    // ASSUMPTION: the names of "id" and "sl" don't exist in MyModel -- if they do, collision
    ...
    return View();
}
0
votes

You could add appropriately named hidden fields in the view and add these as parameters onto the post function, if you want to keep them separate from the model.

Alternatively you should be able to add these onto the model and have them come back out as long as you assign values correctly on the way in.

0
votes

You can add them as named parameters to your controller action, or use the generic FormCollection, which will have all form values.

Alternatively, just add them to your model class, e.g., RegisterModel, and they will be matched by name during model binding.

To keep them separate from your page model, use either:

protected ActionResult Register(RegisterModel model, string parameterName1, int parameterName2)
{
    // parameterName1 and parameterName2 now contain values from the form with the same names
    ...
}

OR

protected ActionResult Register(RegisterModel model, FormCollection fc)
{
    // fc now contains all form values
    ...
}
0
votes

You need to use Hidden Fields to carry over the parameters from URL to View to Controller. For Example, if you have say an ID parameter in your model, which you need to pass to your controller, you should have the following in your Registration form in the view.

@Html.HiddenFor(m=>m.ID)

This way, when your form is posted back, your model will be populated with the ID property as well.