[英]ASP.NET MVC Razor set style attribute in foreach
I want to set the width of a div via the style Attribute
(to use the div as a bootstrap progress bar. I want to do this in a foreach
loop我想通过样式
Attribute
设置 div 的宽度(使用 div 作为引导进度条。我想在foreach
循环中执行此操作
@foreach (var item in Items)
{
<tr>
<td>@item.Name</td>
<td>
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="@item.PercentageCompleted" aria-valuemin="0" aria-valuemax="100" style="width: @item.PercentageCompleted;">
</div>
</div>
</td>
</tr>
} `
What happens is that the style attribute
is empty.发生的情况是 style
attribute
为空。 Is there a way to achieve this?有没有办法实现这一目标?
It depends what value your PercentageCompleted
property gives.这取决于您的
PercentageCompleted
属性给出的值。
If it is just a double
number representing % (eg 50.1
), you will need to append the %
symbol to the end of your @item.PercentageCompleted
like this:如果它只是代表 % 的
double
数(例如50.1
),则需要将%
符号附加到@item.PercentageCompleted
的末尾,如下所示:
... style="width: @(item.PercentageCompleted)%;">
wrapping the Razor content in parentheses to avoid errors.将 Razor 内容括在括号中以避免错误。 This would give you
style="width: 50.1%"
这会给你
style="width: 50.1%"
If you are wanting to set the value in pixels, you will need to do the same, but add px
to the end of your Razor code:如果您想以像素为单位设置值,则需要执行相同的操作,但将
px
添加到 Razor 代码的末尾:
... style="width: @(item.PercentageCompleted)px;">
Most CSS attributes do not accept vanilla numbers (with the exception of 0
);大多数 CSS 属性不接受普通数字(
0
除外); it needs to have a type defined to quantify what measurements to use the value for eg px
, %
, em
, vh
etc.它需要定义一个类型来量化使用值的测量值,例如
px
、 %
、 em
、 vh
等。
View this link to see more information on CSS units.查看此链接以查看有关 CSS 单位的更多信息。 Very useful.
很有用。
It seems to be a culture problem.好像是文化问题。 Using a ',' as decimal seperator in the with does not work, using a '.'
在 with 中使用“,”作为十进制分隔符不起作用,使用“.” works.
作品。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.