title: Can I pass a view model to action link to generate a route?
body: <p>I need to create a link that will be based on my search criteria. For example:</p>
  
  <pre><code>localhost/Search?page=2&amp;Location.PostCode=XX&amp;Location.Country=UK&amp;IsEnabled=true 
  </code></pre>
  
  <p>Parameters in this link are the values of properties in my SearchViewModel.</p>
  
  <p>Ideally I'd like to have something on the lines of:</p>
  
  <pre><code>@Html.ActionLink("Search","User", Model.SearchCriteria)
  </code></pre>
  
  <p>Is this supported by default or do I need to pass properties of my view model into RouteValueDictionary type object and then use that?</p>
  
  <p>My goal is to write a paging helper which would generate page numbers and append the search criteria parameters to the generated links.</p>
  
  <p>E.g.</p>
  
  <pre><code>@Html.GeneratePageLinks(Model.PagingInfo, x =&gt; Url.Action("Index"), Model.SearchCriteria)
  </code></pre>
  
  <p>I've combined your solutions with suggestion from PRO ASP.NET MVC 3 book and ended up with the following:</p>
  
  <p>Helper for generating links. Interesting part is pageUrlDelegate parameter which is later used to invoke Url.Action for generating links:</p>
  
  <pre><code>public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfoViewModel pagingInfo, 
                                          Func&lt;int,String&gt; pageUrlDelegate)
  {
      StringBuilder result = new StringBuilder();
      for (int i = 1; i &lt;= 5; i++)
      {
          TagBuilder tagBuilder = new TagBuilder("a");                                
          tagBuilder.MergeAttribute("href", pageUrlDelegate(i));
          tagBuilder.InnerHtml = i.ToString();
          result.Append(tagBuilder.ToString());               
      }            
  
      return MvcHtmlString.Create(result.ToString());
  }   
  </code></pre>
  
  <p>Then in the view model:</p>
  
  <pre><code>@Html.PageLinks(Model.PagingInfo, x =&gt; Url.Action("Index","Search", new RouteValueDictionary()
      {
              { "Page", x },
              { "Criteria.Location.PostCode", Model.Criteria.Location.PostCode },   
              { "Criteria.Location.Town", Model.Criteria.Location.Town},
              { "Criteria.Location.County", Model.Criteria.Location.County}
      }))
  )
  </code></pre>
  
  <p>I'm still not happy with property names in Strings, but It'll have to do for now.</p>
  
  <p>Thank you :)</p>