ASP.NET MVC 2 点返回链接到适当的视图

发布于 2024-09-25 07:49:02 字数 728 浏览 3 评论 0原文

ASP.NET MVC 2 生成的默认支架视图包含以下链接:

<%: Html.ActionLink("Back to List", "Index") %>
<%: Html.ActionLink("Create New", "Create") %>

如果我从同一根目录访问此页面,这些链接是完美的。但例如,如果我有订单和人员,并且我通过 /Persons/Orders/3 导航到 /Orders/Edit/17,则“返回列表”将返回我到订单根而不是人员根,我想去那里,因为“编辑订单”视图只知道订单。这使得导航变得尴尬并破坏了流程。

我想重用相同的“编辑订单”视图,无论我来自哪里,但我不确定如何传递此信息。

我知道可以传递 /Orders/Edit/17?myparam=myvalue 这样的参数,但是如果我需要传递指示网格排序/过滤顺序的参数,这会限制我以后的选择吗?

将返回/起始位置传递到我的视图以便它可以正确呈现链接的首选方法是什么?否则,如何以不同于控制器的方式调用视图?

编辑:

有关干净的解决方案,请参阅 此帖子< /a>

The default scaffolded views generated by ASP.NET MVC 2 contain links such as:

<%: Html.ActionLink("Back to List", "Index") %>
<%: Html.ActionLink("Create New", "Create") %>

These links are perfect if I came to this page from that same root. But for example, if I have Orders and Persons and I navigate to /Orders/Edit/17 via /Persons/Orders/3, then 'back to list' returns me to Orders root not Persons root, where I want to go, because the 'Edit Orders' view only knows about orders. This makes the navigation awkward and breaks the flow..

I want to reuse the same 'Edit Orders' view regardless of where I came from, but I'm not sure how to pass this information.

I know it's possible to pass parameters like /Orders/Edit/17?myparam=myvalue but will this limit my choices later on if I need to pass parameters which indicate Sort/Filter order for grids?

What is the preferred way to pass a return/origin location to my view so that it can render the links properly? Otherwise, how can I call the view differently from the controller?

EDIT:

For a clean solution, see THIS POST

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

南汐寒笙箫 2024-10-02 07:49:02

只要不使用相同的名称,通过查询字符串传递参数并不会真正限制您。查询字符串有大小限制,但您可能不会达到它。

我基本上就是这样做的。我很好奇看看其他人的回答。

Passing in parameters through the querystring will not really limit you as long as you don't use the same names. There is a size restriction on querystrings, but you'll probably not hit it.

That's basically how I do it. I'm curious to see what others answer.

傲世九天 2024-10-02 07:49:02

这个功能似乎应该在框架中考虑到;必须指定这些参数似乎相当麻烦,因为您必须为每个导航级别都这样做。

为什么? back 是与历史一起属于浏览器的东西。这种情况已经有一段时间了,为什么框架需要处理这个问题?

你需要的不是常态。

此外,您有责任保持代码干燥。您绝对可以通过一种方式来处理它,即您所拥有的所有重复都是您正在调用的函数的名称。

This functionality seems like something that should have been accounted for in the framework; it seems fairly hacky to have to specify these parameters because you'd have to do so for every level of navigation.

Why? back is something that belongs in browsers, along with history. Its been like that for a while, why should the framework need to handle that?

What you need its not the norm.

Additionally, you are responsible to keep your code dry. You can definitely handle it in a way that all the repetition you have is the name of the function you are calling.

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