How do I create the correct route values for this ActionLink?

后端 未结 5 1300
一整个雨季
一整个雨季 2021-02-18 21:48

The Model of SearchResults.aspx is an instance of PersonSearch; when the request for a new page arrive (a GET request), the action method should take i

5条回答
  •  梦谈多话
    2021-02-18 21:55

    If you are using Razor (I realize OP asked four years ago before Razor was invented, but people finding this maybe using it).

    I was able to get something working by using an inline @helper method.

    @helper RunnerLink(PersonSearch model, int page)
    {
        var routeParms =new RouteValueDictionary(model.GetType().GetProperties().ToDictionary(p => p.Name, p => p.GetValue(model, null)));
        routeParms.Add("page", page.ToString());
        routeParms.Add("Controller", "Property");
        @Html.ActionLink("Search", "Index", routeParms)
    }
    

    Usage would be simple --

    @RunnerLink(myPersonSearchInstance, 1)
    

    It isn't the most elegant solution, but it works well if you want to pass an object in as a routeValue, but you need to pass additional items, such as Controller, Area or in OPs case page.

提交回复
热议问题