tags:

views:

42

answers:

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?

+1  A: 

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.

Simon Steele
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!!!
Wajih