1

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.

2
  • why do you need js there in the first place? Commented Jun 18, 2015 at 7:28
  • Did any of the answers work out for you? Commented Jun 18, 2015 at 8:09

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

now it's returning "[email protected](Convert.ToDouble(ViewBag.lst[0].ca / 2))"
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.