Reputation:
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?
Upvotes: 1
Views: 2940
Reputation: 11608
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.
Upvotes: 1