在 MVC 中构建链接的最佳实践
如果我不想在视图标记中执行此操作,
<%=Html.RouteLink("列表", "列表", new {market = "奥斯汀", state = "德克萨斯", 页码 = 3 })%>
在 MVC 2 中创建链接的最佳方法是什么?我有一个 HTML 帮助器来构建自定义链接,但它所做的只是充当 RouteLink(或 ActionLink)的包装器。我想要一些更优雅且语法更短的东西。如果我在控制器中构建链接,如何将其推送到视图中?通过查看数据? (希望不是)。或者它会成为 ViewModel 的一部分吗?
If I do not want to do this in my View Markup,
<%=Html.RouteLink("Listings", "Listings", new {market = "Austin", state = "Texas", pagenumber = 3 })%>
what would be the best approach to creating links in MVC 2? I have a HTML helper to build a custom link but all it is doing is acting as a wrapper around RouteLink (or ActionLink). I would want something more elegant and one that has a shorter syntax. If I build my links in a controller, how do I push it into the view? Via ViewData? (hope not). Or would it be part of the ViewModel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与 MVC Futures ActionLink 助手相比,我更喜欢使用 T4MVC。您的行看起来像这样:
http://mvccontrib.codeplex.com/wikipage?title=T4MVC
I like using T4MVC better than MVC Futures ActionLink helpers. You line would look like this:
http://mvccontrib.codeplex.com/wikipage?title=T4MVC
从长远来看,我认为您在 MVC Futures 库中看到的 Html.ActionLink 是飞翔的方式:
使用您的 HtmlHelpers,即使它们只是当前基于魔术字符串的功能的包装器,因为它至少减少了您的魔术前景字符串并提供单点更改。
如果我能帮助后端构建语法糖,我就不会参与构建链接。
Long term, I think the Html.ActionLink which you see in the MVC Futures library is the way to fly:
Using your HtmlHelpers, even if they are just wrappers around the current magic-string based functionality as it at least reduces your frontage to magic strings and provides a single point to change.
I wouldn't get involved in building links on the back-end if I could help it in an effort to make syntactical sugar.