Reputation: 369
How do I pass a whole model via html.actionlink
or using any other method except form submission? Is there any way or tips for it?
Upvotes: 13
Views: 20273
Reputation: 32768
Though it's not advisable in complex cases, you can still do that!
public class QueryViewModel
{
public string Search { get; set; }
public string Category { get; set; }
public int Page { get; set; }
}
// just for testing
@{
var queryViewModel = new QueryViewModel
{
Search = "routing",
Category = "mvc",
Page = 23
};
}
@Html.ActionLink("Looking for something", "SearchAction", "SearchController"
queryViewModel, null);
This will generate an action link with href
like this,
/SearchController/SearchAction?Search=routing&Category=mvc&Page=23
Here will be your action,
public ViewResult SearchAction(QueryViewModel query)
{
...
}
Upvotes: 17
Reputation: 5606
You could use javascript to detect a click on the link, serialize the form (or whatever data you want to pass) and append it to your request parameters. This should achieve what you're looking to achieve...
Upvotes: 1
Reputation: 1038850
No, you cannot pass entire complex objects with links or forms. You have a couple of possible approaches that you could take:
Upvotes: 4