[英]How to check what rendered partial view?
I would like to render common data within these div elements. 我想在这些div元素中呈现通用数据。
<div id=element1>
@Htm.Partial("CommonView")
@*Other data...*@
</div>
<div id=element2>
@Htm.Partial("CommonView")
@*Some other data...*@
</div>
<div id=element3>
@Htm.Partial("CommonView")
@*More other data...*@
</div>
My common view looks something like this 我的共同观点是这样的
<input id="txtSomethingElse" class="input"/>
I can't have elements with same ids on the same page, but I need to be able to get values from input
element. 我不能在同一页面上使用具有相同ID的元素,但是我需要能够从input
元素中获取值。 How can I implent some Razor logic that will add a prefix or sufix to txtSomethingElse
我如何才能使一些Razor逻辑成为现实,这些逻辑会为txtSomethingElse
添加前缀或后缀
I would like it to be rendered like this 我希望这样渲染
<div id=element1>
<input id="txtSomethingElse_1" class="input"/>
</div>
<div id=element2>
<input id="txtSomethingElse_2" class="input"/>
</div>
<div id=element3>
<input id="txtSomethingElse_3" class="input"/>
</div>
you can easily use this over load of the Html.Partial method 您可以轻松使用Html.Partial方法的重载
PartialExtensions.Partial Method (HtmlHelper, String, Object)
http://msdn.microsoft.com/en-us/library/ee402926(v=vs.108).aspx http://msdn.microsoft.com/zh-CN/library/ee402926(v=vs.108).aspx
allowing you to pass a model to the partial view which in your case can be an int denoting an index 允许您将模型传递给局部视图,在您的情况下,局部视图可以是表示索引的整数
so you partial views changes to <input id="txtSomethingElse_@Model.ToString()" class="input"/>
where Model is of type int
因此您将部分视图更改为<input id="txtSomethingElse_@Model.ToString()" class="input"/>
,其中Model的类型为int
and your code where you are to render this view will change to 并且您要呈现此视图的代码将更改为
<div id=element1>
@Htm.Partial("CommonView", 1)
@*Other data...*@
</div>
<div id=element2>
@Htm.Partial("CommonView", 2)
@*Some other data...*@
</div>
<div id=element3>
@Htm.Partial("CommonView", 3)
@*More other data...*@
</div>
you can do something like, say your model had an Id Property 您可以执行类似的操作,例如说您的模型具有Id属性
<input id="txtSomethingElse@Model.Id" class="input" />
I'm not too good at razor, but I know the old school syntax would work 我不太擅长剃须刀,但是我知道老式的语法会起作用
<input id="txtSomethingElse<%: Model.Id %>" class=input />
Couldn't you do: 你不能做:
@Html.InputFor(m => m.FirstThing, new { @id = @Model.FirstThing.Id + "txtSomethingElse" })
@Html.InputFor(m => m.SecondThing, new { @id = @Model.SecondThing.Id + "txtSomethingElse" })
...
More readable/easier than other answers. 比其他答案更具可读性/更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.