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

后端 未结 3 1094
别跟我提以往
别跟我提以往 2020-12-14 21:53

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?

3条回答
  •  有刺的猬
    2020-12-14 22:07

    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)
    {
       ...
    }
    

提交回复
热议问题