获取变量PartialView [英] Get variables in PartialView
问题描述
说我将数据传递给像这样
Say I pass data to a partial view like this
@Html.Partial("_LandingPage_CaseStudiesList", new { TrackAction = "Case Study Click", CaseStudies = Model.CaseStudies })
我如何再拿到了部分的数据?
How do I then get the data in the partial?
我曾尝试模型[TrackAction]
和 Model.TrackAction
但是没有工作,我不知道知道什么尝试。
I have tried Model["TrackAction"]
and Model.TrackAction
but neither work and I don't know what else to try.
如果我调试到code我可以看到属性,但就是不知道用什么来让他们回来
If I debug into the code I can see the properties but just don't know what to use to get them back
下面是我的部分。
<ul id="case-studies-list" class="table">
@{
int counter = 1;
foreach (Asset caseStudy in caseStudies)
{
<li class="@(counter == 1 ? "first " : string.Empty)cell">
<a href="@caseStudy.Url" class="track-event" data-action="@trackAction" data-label="Case Study Download" data-value="@caseStudy.Name" target="_blank">
<span class="sprite @string.Format("case-study{0}", counter)"></span>
<span class="text">
@Html.Raw(caseStudy.Name)<br />
<span class="font11">@Html.Raw(caseStudy.Location)</span>
</span>
</a>
</li>
counter++;
}
}
</ul>
这是我所得到的时候我调试的映像: http://i.imgur.com/ OiReWZv.gif
This is an image of what I get when I debug: http://i.imgur.com/OiReWZv.gif
推荐答案
您可以传递一个匿名对象作为模型的局部视图,你在你的问题有办法:
You can pass an anonymous object as model to a partial view the way you have in your question:
@Html.Partial("_LandingPage_CaseStudiesList", new { TrackAction = "Case Study Click", CaseStudies = "fsdffd" })
然后你有两个选择你的部分观点:
Then you have a couple of options in your partial view:
-
不要在您的视图声明的模型(即不包括声明
像@model为MyModel
在顶部)
声明模式为动态的(即包括像 @model动态声明
)
Declare the model as dynamic (i.e. include a statement like @model dynamic
)
在这两种情况下,你应该能够访问你的属性,如
In both cases you should be able to access your properties as in
@Model.TrackAction
@Model.CaseStudies
此外,你会不会对你的视图文件智能感知,如果属性不存在于模型中,您将收到运行时异常。
Also, you will not have intellisense on your view file and you will receive an exception at runtime if the property doesn't exist in the model.
这篇关于获取变量PartialView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!