Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to pass a whole model via html.actionlink in ASP.NET MVC 3?

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?

like image 534
kiransh Avatar asked Jul 22 '12 06:07

kiransh


People also ask

How do I pass model value in ActionLink?

ActionLink is rendered as an HTML Anchor Tag (HyperLink) and hence it produces a GET request to the Controller's Action method which cannot be used to send Model data (object). Hence in order to pass (send) Model data (object) from View to Controller using @Html.

What is difference between HTML ActionLink and URL action?

There is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.

What is HTML ActionLink in ASP NET MVC?

Html. ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.


2 Answers

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)
{
   ...
}
like image 150
VJAI Avatar answered Sep 29 '22 16:09

VJAI


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
like image 20
Darin Dimitrov Avatar answered Sep 29 '22 17:09

Darin Dimitrov