简体   繁体   中英

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

<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.

But I am getting " a+3 " or " 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.

The + is not evaluated as operator rather treated as a string , you can use string.Concat to concatenate the string and your expression .

 <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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM