6

In my MVC3 app, if i type in a query string value in the url and hit enter, I can get the value i typed:

localhost:34556/?db=test

My default action which will fire:

public ActionResult Index(string db)

The variable db has "test" in it.

Now, i need to submit a form and read the query string value, but when I submit the form via jQuery:

       $('#btnLogOn').click(function(e) {
         e.preventDefault();
             document.forms[0].submit();
         });

And the following is the form i'm sending:

   @using (Html.BeginForm("LogIn", "Home", new { id="form1" }, FormMethod.Post))

Heres the action:

  [HttpPost]
    public ActionResult LogIn(LogOnModel logOnModel, string db)
    {
        string dbName= Request.QueryString["db"];
     }

The variable dbName is null because Request.QueryString["db"] is null, so is the variable db being passed in and i dont know why. Can someone help me get a query string variable after a form is submitted? Thanks

BoundForGlory
  • 4,114
  • 15
  • 54
  • 81
  • 1
    Query string will not be visible in a form post: http://stackoverflow.com/questions/1116019/submitting-a-get-form-with-query-string-params-and-hidden-params-disappear – Davin Tryon Jul 09 '12 at 20:40
  • Thanks for replying, then what is my alternative? I have to keep the query string btw – BoundForGlory Jul 09 '12 at 20:50
  • The alternative is to use the query string in the GET to populate a hidden input in a form. Then the hidden input will post in with a submit. – Davin Tryon Jul 09 '12 at 20:53

3 Answers3

4

You could have something like

Controllers:

[HttpGet]
public ActionResult LogIn(string dbName)
{
    LogOnViewModel lovm = new LogOnViewModel();
    //Initalize viewmodel here
    Return view(lovm);

}

[HttpPost]
public ActionResult LogIn(LogOnViewModel lovm, string dbName)
{
    if (ModelState.IsValid) {
        //You can reference the dbName here simply by typing dbName (i.e) string test = dbName;
        //Do whatever you want here. Perhaps a redirect?
    }
    return View(lovm);
}

ViewModel:

public class LogOnViewModel
{
    //Whatever properties you have.
}

Edit: Fixed it up for your requirement.

Saedeas
  • 1,548
  • 8
  • 17
  • Thanks for commenting. This looks very similar to what im doing now. Where is the logic to get the value from the query string? – BoundForGlory Jul 09 '12 at 21:05
  • Err, the point of this method was that you don't have the value in the query string, you just assign it to your viewmodel inside the form. Something like @Html.Textboxfor(model => model.dbName, etc.). If you really want to put it in the URL (SEO purposes?) I think you can add a parameter in the @using(Html.BeginForm("LoginForm", "Home", new {.id = "form1", .dbName = YourFormFieldValueHere}, FormMethod.Post)) – Saedeas Jul 09 '12 at 21:08
  • Err, my question was how do i get the query string value? Not any alternate ways. If i could go another route i would. But business rules say we use query string and i dont own the company. Thanks anyway – BoundForGlory Jul 09 '12 at 21:15
  • Try my edit. The comments under the HTTPPost part should allow you to access the dbName part of the URL. You don't need to do a Request. Simply set whatever var you want equal to dbName. – Saedeas Jul 09 '12 at 21:31
3

Since you are using POST the data you are looking for is in Request.Form instead of Request.QueryString.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
2

As @ThiefMaster♦ said, in a POST you cannot have the query string, never the less if you don't wan't to serialize your data to an specific object you can use the FormCollection Object which allows you to get all the form elements pass by post to the server

For example

[HttpPost]
public ActionResult LogIn(FormCollection formCollection)
{
    string dbName= formCollection["db"];

}
Jorge
  • 17,896
  • 19
  • 80
  • 126
  • Will the FormCollection object have my logon information? the model being passed in has username/password etc.. – BoundForGlory Jul 09 '12 at 20:59
  • It Contains all the elements that you had passed in the form, if your logon information in inside of the form, it will be on the formCollection – Jorge Jul 09 '12 at 21:00