0

Within an MVC 5 app, I have a view with an ActionLink like this:

@Html.ActionLink("Publish", "Index", "Publish", item, null)

When I hover over the link, the URL shows that the following will be sent to the view as parameters:

?Title=FilePicker&Language=JavaScript

How do I access those parameters so that I can display them on the second view?

For example, I'm trying to do the following, but do not know what needs to go into the Value attribute:

<input type="text" class="form-control" id="search" value="NEED TO ACCESS TITLE PARAM HERE" />

3 Answers 3

3

Use:

<input type="text" class="form-control" id="search" value="@Request.QueryString["Title"]" />

For a cleaner separation between models, view, and controllers it would be recommended that the controller send the title to the view. But this is fine as well.

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

1 Comment

I'd consider this too brittle for an MVC app, since if the URL is updated to take one of the parameters, it will be removed from the query string portion, and this code no longer works.
1

Your best option would be to bind these values to action parameters in the Index action and add them to the view model. Some variation of the following should see you through:

public ActionResult Index(string title, string language)
{
    var model = new PublishIndexViewModel { Title = title, Language = language };
    return View(model);
}

Then in your view:

@model PublishIndexViewModel

...

<input type="text" class="form-control" id="search" value="@Model.Title" />

This more in-keeping with ASP.NET MVC's "best practice" patterns than - and is preferable to - handling Request.QueryString directly and can be easily accommodated into any existing view models you already have.

5 Comments

When I change the controller as you describe, I get runtime errors due to a parameter-less controller constructor.
@StraxTillbaka As I said, some variation of. This is the pattern you should be following - I can't guarantee that this is the only change you'll need to make as I don't have access to your entire codebase. That's a separate issue that is beyond the scope of the question and that you should be able to address yourself.
That is a strange error to encounter though, are you sure you're putting these parameters on the action method and not on a constructor?
I modified the Index action method to take the object that is being used in the first view. I'm sure I'm doing something silly and will keep at it.
The problem turned out to be that the object being passed to the view needed a parameterless c'tor. I modified the code to work within the best practices guidelines you suggested and all now works great. Thanks!
0

@AntP is correct. The preferred method for MVC is to have the action accept the parameters. However, failing that, you should use:

Request.Unvalidated["myParam"]

Which will always return the parameter whether it was passed as part of the URL path or query string, and whether or not it was accepted as a parameter to the action method. It's pretty much a catch all that will guarantee the value is returned no matter how it was handled.

The .Unvalidated portion is to turn off model validation which is kind of stupidly run any time you try to pull anything from just the Request object, by itself.

Comments

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.