我的应用程序中有几个共享的剃刀部分视图,以显示不同页面上的产品的一些细节。现在我需要更改部分视图中的一些链接,具体取决于呈现部分视图的主视图。
例如:
如果在" index.cshtml"内部呈现部分视图,则部分视图中的一个链接应为:
<a href="www.site1.com">site1 called from index.cshtml</a>
如果它在&#34; insert.cshtml&#34;然后链接需要
<a href="www.someothersite2.com">other site that's not 1 and it's called from insert.cshtml</a>
类似的东西:
@if (something.parentview = "index.cshtml")
{ <a href="www.site1.com">site1 called from index.cshtml</a>}
else {
<a href="www.someothersite2.com">other site that's not 1 and it's called from insert.cshtml</a>
}
有办法做到这一点吗?
答案 0 :(得分:0)
当你调用Shared PartialView时,你知道你在哪里。 在呈现时依赖于“检测”呈现哪个视图的内部更容易将参数传递给部分。想象一下......你有3个级别的嵌套......事情变得更加复杂。想象一下,如果你改变路由或视图名称?
您可以使用ViewBag传递参数:在视图中,您可以设置要在Partial内部检查的内容。不需要控制器代码。
更强大的方法是将ViewModel传递给Partial,但如果您需要传递的唯一数据只是一个参数,则ViewBag更简单。