[英]concatenating value to value in c# code — Asp.net MVC
I have an anchor and I am assigning the the id to this anchor dynamically 我有一个锚点,我正在动态地为这个锚点分配id
<li>
<a href="#"
name="offset" onclick="return so(this);"
data-val="@Math.Round(Convert.ToDouble(ViewBag.lst[0].ca/2))"
id='javascript:"a+@Math.Round(Convert.ToDouble(ViewBag.lst[0].ca / 2))"'>Last</a>
</li>
I supposed to get a3 or a4 or a5 because this @Math.Round(Convert.ToDouble(ViewBag.lst[0].ca / 2))
returns numeric value. 我应该得到a3或a4或a5因为这个
@Math.Round(Convert.ToDouble(ViewBag.lst[0].ca / 2))
返回数值。
But I am getting " a+3
" or " a+4
". 但我得到的是“
a+3
”或“ a+4
”。 Apparently it is concatenating the plus sign too. 显然它也是加号的连接。
What I am trying to do above is simple string concatenation. 我上面要做的是简单的字符串连接。 This above code is from asp.net mvc view.
以上代码来自asp.net mvc视图。
The +
is not evaluated as operator rather treated as a string , you can use string.Concat
to concatenate the string and your expression . +
不被视为运算符而是被视为字符串 ,您可以使用string.Concat
来连接字符串和表达式 。
<li><a href="#" name="offset" onclick="return so(this);" data-val="@Math.Round(Convert.ToDouble(ViewBag.lst[0].ca/2))"
id='@string.Concat("a",Math.Round(Convert.ToDouble(ViewBag.lst[0].ca / 2)))'>Last</a></li>
因为你在你的cshtml(我想)中这样做,你可以避免内联javascript的丑陋,只需使用:
@("a"+ Math.Round(Convert.ToDouble(ViewBag.lst[0].ca / 2))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.