在 MVC 中构建链接的最佳实践

发布于 2024-09-12 21:43:12 字数 307 浏览 1 评论 0原文

如果我不想在视图标记中执行此操作,

<%=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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见 2024-09-19 21:43:12

与 MVC Futures ActionLink 助手相比,我更喜欢使用 T4MVC。您的行看起来像这样:

<%:Html.ActionLink("Listings", MVC.Listings.Index("Austing", "Texas", 3)) %>

http://mvccontrib.codeplex.com/wikipage?title=T4MVC

I like using T4MVC better than MVC Futures ActionLink helpers. You line would look like this:

<%:Html.ActionLink("Listings", MVC.Listings.Index("Austing", "Texas", 3)) %>

http://mvccontrib.codeplex.com/wikipage?title=T4MVC

灯角 2024-09-19 21:43:12

从长远来看,我认为您在 MVC Futures 库中看到的 Html.ActionLink 是飞翔的方式:

<%= Html.ActionLink<ListingsController>(x => x.Listings("Austin", "Texas", 3)) %>

使用您的 HtmlHelpers,即使它们只是当前基于魔术字符串的功能的包装器,因为它至少减少了您的魔术前景字符串并提供单点更改。

如果我能帮助后端构建语法糖,我就不会参与构建链接。

Long term, I think the Html.ActionLink which you see in the MVC Futures library is the way to fly:

<%= Html.ActionLink<ListingsController>(x => x.Listings("Austin", "Texas", 3)) %>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文