user786
user786

Reputation: 4364

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:"[email protected](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.

Upvotes: 1

Views: 964

Answers (2)

Adil
Adil

Reputation: 148110

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>

Upvotes: 2

noobed
noobed

Reputation: 1339

Since you are doing this inside your cshtml (I suppose), you could avoid the ugliness of inline javascript and simply use:

 @("a"+ Math.Round(Convert.ToDouble(ViewBag.lst[0].ca / 2))

Upvotes: 1

Related Questions