ASP.NET MVC 2 点返回链接到适当的视图
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只要不使用相同的名称,通过查询字符串传递参数并不会真正限制您。查询字符串有大小限制,但您可能不会达到它。
我基本上就是这样做的。我很好奇看看其他人的回答。
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.
为什么? back 是与历史一起属于浏览器的东西。这种情况已经有一段时间了,为什么框架需要处理这个问题?
你需要的不是常态。
此外,您有责任保持代码干燥。您绝对可以通过一种方式来处理它,即您所拥有的所有重复都是您正在调用的函数的名称。
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.