1

I want to pass multiple values to a controller. The controller looks like

Page(string type, string keywords, string sortType)

In a asp.net page,

I have

<%=Url.Action("Page", "Search", new { type = "new",keywords = keywords, sortType = "Date" }) %>

But the values for type and sorType are passed as null inside the controller.

What am I doing wrong here?

3
  • What URL does the Url.Action generate? Also check your routes. Commented Sep 22, 2010 at 11:25
  • Routes are fine, I only have one route it does enter there. Keywords are passing fine as string, but type and sortType both are null. Commented Sep 22, 2010 at 11:29
  • Ok, when you view the page with that link, what does the generated link actually look like, e.g. http://blah/Search/Page/?type=new&keywords=blahblah&sortType=Date Commented Sep 22, 2010 at 11:30

1 Answer 1

1

I've just double-checked, and this should work fine. I created this controller method in a new MVC app's Home controller:

public ActionResult Page(string type, string keywords, string sortType)
{
    this.ViewData["Type"] = type;
    this.ViewData["Keywords"] = keywords;
    this.ViewData["SortType"] = sortType;
    return this.View("Index");
}

and then added this to the Index view:

<ul>
<% foreach (var item in ViewData) { %>
    <li><%: item.Key %> = <%: string.IsNullOrEmpty(item.Value as string) ? "null" : item.Value %></li>
<% } %>
</ul>

<%: Html.ActionLink("Hello", "Page", "Home", new { type = "new", keywords = "blahblah", sortType = "Date" }, null) %>

The page correctly displays the following after clicking the "Hello" link:

o Type = new
o Keywords = blahblah
o SortType = Date

So if this works in a simple new MVC app, I think it must be either other methods in your controller or a routing problem causing it.

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

1 Comment

The solution was to create a string and then consume it<% string sType = "Date";%> <%=Url.Action("Page", "Search", new { type = "new",keywords = keywords, sortType = sType }) %> Otherwise i would keep getting null. and that is ridiculous!!!

Your Answer

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