2

I have an MVC3 view with a form and i want to pass in a parameter via query string. Heres my form

@using (Html.BeginForm("LogOn", "Account", new { db = @Request.QueryString["db"] }, FormMethod.Post))

Heres my Controller

[HttpPost]
public ActionResult LogOn(LogOnModel model,string db)

When I click my Login button, and step through the code, the param db is null. Im passing it in like this:

  http://localhost:64632/?db=someValue

My route looks like this:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{db}", // URL with parameters
        new { controller = "Account", action = "LogOn", db = UrlParameter.Optional           } // Parameter defaults
);

What am i doing wrong?

5
  • Why can't you just put db as a property in your Model, set it on load of that page (it must be set in the query string somewhere), then put it in a hidden field, that way when you submit the form you have the value. Default MVC model binding FTW :) Commented May 23, 2012 at 20:44
  • This is a business requirement. The current app accepts db as a query string parameter and the client doesnt want to change that Commented May 23, 2012 at 20:45
  • 1
    I was wondering if we could use @Request.QueryString["db"] in the view. Did you check whether that value was set correctly? Commented May 23, 2012 at 20:59
  • Hmmm.... interesting; however for me I am not able to recreate the issue. Your code works 100% fine. BTW what about your model; does it have values? Commented May 23, 2012 at 22:38
  • Yes my model has values (username password etc) Commented May 24, 2012 at 13:04

4 Answers 4

1

Your route is expecting db to be a token in the url:

http://localhost:64632/Account/LogOn/someValue

You could grab the value from the querystring in your controller and store the db parameter in a ViewBag and it will be available on your form.

Sign up to request clarification or add additional context in comments.

1 Comment

I wish i could, but this is a business requirement. Plus i thought with MVC, a query string is the same as what you described
1

If that's the only route you have, then presumably the page you're currently on looks like "/controller/action/dbValue". In this case,

@Request.QueryString["db"]

won't fetch anything because db is a route value, it's not part of the query string. You could use

@ViewContext.RouteData.Values["db"]

instead.

Comments

1

It works. I am doing the same thing. I can retrieve the query string values on a http post: Request["queryparametername"]

Comments

0

well, this is a get method link : "http://localhost:64632/?db=someValue"

and you used the HttpPost attribute.

1 Comment

Methods using the HttpPost attribute can still receive parameters from a query string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.