我可以将视图模型传递给操作链接来生成路线吗?
我需要创建一个基于我的搜索条件的链接。例如:
localhost/Search?page=2&Location.PostCode=XX&Location.Country=UK&IsEnabled=true
此链接中的参数是我的 SearchViewModel 中的属性值。
理想情况下,我希望有以下内容:
@Html.ActionLink("Search","User", Model.SearchCriteria)
这是默认支持的还是我需要将视图模型的属性传递到 RouteValueDictionary 类型对象中然后使用它?
我的目标是编写一个分页助手,它将生成页码并将搜索条件参数附加到生成的链接中。
例如,
@Html.GeneratePageLinks(Model.PagingInfo, x => Url.Action("Index"), Model.SearchCriteria)
我将您的解决方案与 PRO ASP.NET MVC 3 书中的建议相结合,最终得到以下结果:
用于生成链接的帮助程序。有趣的部分是 pageUrlDelegate 参数,稍后用于调用 Url.Action 来生成链接:
public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfoViewModel pagingInfo,
Func<int,String> pageUrlDelegate)
{
StringBuilder result = new StringBuilder();
for (int i = 1; i <= 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());
}
然后在视图模型中:
@Html.PageLinks(Model.PagingInfo, x => 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}
}))
)
我仍然对字符串中的属性名称不满意,但现在必须这样做。
谢谢 :)
I need to create a link that will be based on my search criteria. For example:
localhost/Search?page=2&Location.PostCode=XX&Location.Country=UK&IsEnabled=true
Parameters in this link are the values of properties in my SearchViewModel.
Ideally I'd like to have something on the lines of:
@Html.ActionLink("Search","User", Model.SearchCriteria)
Is this supported by default or do I need to pass properties of my view model into RouteValueDictionary type object and then use that?
My goal is to write a paging helper which would generate page numbers and append the search criteria parameters to the generated links.
E.g.
@Html.GeneratePageLinks(Model.PagingInfo, x => Url.Action("Index"), Model.SearchCriteria)
I've combined your solutions with suggestion from PRO ASP.NET MVC 3 book and ended up with the following:
Helper for generating links. Interesting part is pageUrlDelegate parameter which is later used to invoke Url.Action for generating links:
public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfoViewModel pagingInfo,
Func<int,String> pageUrlDelegate)
{
StringBuilder result = new StringBuilder();
for (int i = 1; i <= 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());
}
Then in the view model:
@Html.PageLinks(Model.PagingInfo, x => 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}
}))
)
I'm still not happy with property names in Strings, but It'll have to do for now.
Thank you :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,这是不可能的。您必须一一传递属性。您确实可以使用带有
RouteValueDictionary
的重载:当然,最好编写一个自定义 ActionLink 帮助器来执行此操作:
然后:
另一种可能性是仅传递
id
> 并让控制器操作从您最初在渲染此视图的控制器操作中获取模型和值的位置获取相应的模型和值。Unfortunately that's not possible. You will have to pass properties one by one. You could indeed use the overload which takes a
RouteValueDictionary
:Of course it's probably better to write a custom ActionLink helper to do this:
and then:
Another possibility is to pass only the
id
and have the controller action fetch the corresponding model and values from wherever you fetched it initially in the controller action that rendered this view.以下是从 Steve Sanderson 的 Tekpub 示例 中获取的一些代码,大致就是您要查找的内容为了。首先在您的对象上定义一个扩展方法 -
然后使用该方法调用
Html.RouteLink
-Here's some code taken from Steve Sanderson's Tekpub samples that is broadly what you're looking for. First define an extension method on your object -
Then call
Html.RouteLink
using that method -