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?
Asked
Active
Viewed 2.0k times
13

Heidelbergensis
- 475
- 2
- 5
- 18

kiransh
- 369
- 2
- 4
- 15
-
did you try using `ViewData` ? – Yasser Shaikh Jul 22 '12 at 06:30
-
if there such a need, you are probably doing something wrong. Describe what you are trying to achieve. – Dmitry Efimenko Jul 22 '12 at 07:30
3 Answers
17
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)
{
...
}

VJAI
- 32,167
- 23
- 102
- 164
-
tx bro it was a gud tips for me and i had done a bit progress regard to ur post – kiransh Jul 23 '12 at 09:22
-
-
If there are more properties in the class then the URL could become too big and also there are chances some sensitive information could be leaked. – VJAI Jul 13 '17 at 09:30
4
No, you cannot pass entire complex objects with links or forms. You have a couple of possible approaches that you could take:
- Include each individual property of the object as query string parameters (or input fields if you are using a form) so that the default model binder is able to reconstruct the object back in the controller action
- Pass only an id as query string parameter (or input field if you are using a form) and have the controller action use this id to retrieve the actual object from some data store
- Use session

Darin Dimitrov
- 1,023,142
- 271
- 3,287
- 2,928
1
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...

JP.
- 5,536
- 7
- 58
- 100
-
i don't know much about serializaition in javascript and u provide me an example if possible – kiransh Jul 22 '12 at 10:29