如何将ID添加到@ Html.DisplayFor参数? [英] How to add id to @Html.DisplayFor parameter?
问题描述
我正在寻找如何将ID添加到@ Html.DisplayFor的决定,我可以将jQuery方法绑定到该ID.
I looking for decision how to add a id to @Html.DisplayFor, that to this id I could tied jQuery method.
<dd>
@Html.DisplayFor(model => model.Price)
</dd>
感谢您的建议.
推荐答案
DisplayFor
帮助器方法只是呈现要传递给它的表达式的值.因此,假设您的 Price
属性值为 235.34
,razor呈现的标记将为
The DisplayFor
helper method simply renders the value of the expression you are passing to it. So in your case, let's say your Price
property value is 235.34
, The markup rendered by razor will be
<dd>
235.34
</dd>
如果您只想读取 Price
属性的值,则可以考虑将值保留在隐藏的表单元素中.您可以使用 HiddenFor
帮助器方法.
If all you care about reading the value of the Price
property, you may consider keeping the value inside a hidden form element. You can use the HiddenFor
helper method.
<dd>
@Html.DisplayFor(model => model.Price)
@Html.HiddenFor(a=>a.Price)
</dd>
这将呈现以下标记
<dd id="item">
235.34
<input id="Price" name="Price" type="hidden" value="235.34" />
</dd>
您可以看到,现在有了一个ID为 Price
的输入元素.这意味着您可以像这样在jQuery中读取值
You can see that now you have an input element with id Price
. That means you can read the value in jQuery like
var v = $("#Price").val();
另一个选择是将值保留在HTML5数据属性中.
Another option is keeping the value in HTML5 data attributes.
<dd id="price" data-price="@Model.Price">
@Html.DisplayFor(model => model.Price)
</dd>
您可以阅读它
var v = $("#price").data("price");
这篇关于如何将ID添加到@ Html.DisplayFor参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!