[英]ASP.NET MVC jQuery HTML5 data attribute
I have three .cshtml files: _ProductSelection.cstml -> this defines the html table 我有三个.cshtml文件:_ProductSelection.cstml->这定义了html表
@model List<..Rate>
<table id="productgrid">
<thead>
...
</thead>
<tbody>
@Html.Partial("_ProductRows", Model)
</tbody>
</table>
_ProductRows.cshtml-> this has some attributes added to each tr and td _ProductRows.cshtml->这会将一些属性添加到每个tr和td
@if (Model != null && Model.Count > 0)
{
foreach (var rate in Model)
{
<tr class="@(rate.IsSelected ? "selected" : "")" data-base="@rate.BaseCharge" data-planname="@rate.PlanName" >
...
ProductSelection.cshtml->actual view call from MVC controller: MVC控制器发出的ProductSelection.cshtml-> actual view调用:
....
@Html.Partial("_ProductSelection", Model.Rates)
...
@section scripts {
..
var table = $('#productgrid'),
tbody = table.find('tbody'),
...
$(function () {
....
tbody.find('tr').each(function (index, elem) {
var self = $(elem),
base = parseFloat(self.attr('data-base')),
.....
This is working code I can access all the data attribute in productSelection.cshtml, which are set in tr tag of _ProductRows.cshtml file. 这是工作代码,我可以访问productSelection.cshtml中的所有data属性,这些属性在_ProductRows.cshtml文件的tr标签中设置。 eg data-base, data-planname
例如数据库,数据计划名称
The problem is: I added some more data attribute to _ProductRows tr tag, but new data attributes are not visible in ProductSelection.cshtml, when i debug in google chrome and check value of elem it only has data attributes define before, newly added attribute in not in 'attribute' list of elem. 问题是:我在_ProductRows tr标记中添加了更多数据属性,但是在ProductSelection.cshtml中看不到新的数据属性,当我在google chrome中调试并检查elem的值时,它仅具有之前定义的数据属性,而在不在元素的“属性”列表中。 In bellow example i don't see newly added data-credit.
在下面的示例中,我看不到新添加的数据信用。
<tr class="@(rate.IsSelected ? "selected" : "")" data-base="@rate.BaseCharge" data-planname="@rate.PlanName" data-credit="@rate.Credit" >
I added almost 20 more data attribute, none of them are visible while debugging in chrome. 我增加了将近20个数据属性,在chrome中调试时它们都不可见。 in Rate model I added all these new fields and populated it from values from database.
在费率模型中,我添加了所有这些新字段,并从数据库中的值填充了它。
thank you everyone for response. 谢谢大家的回应。
I found out the reason why new attributes are not appearing in "elem". 我找出了新属性未出现在“元素”中的原因。
on ProductSelection.cshtml there is one more function which added attributes to tr tag, which overwrites the attributes added in _ProductRows.cshtml. 在ProductSelection.cshtml上,还有一个函数将属性添加到tr标签,该函数将覆盖_ProductRows.cshtml中添加的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.