简体   繁体   English

在c#代码中连接值到值 - Asp.net MVC

[英]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.

相关问题 无法将值添加到列表C#MVC ASP.NET - Cant add value to list c# mvc asp.net 在C#代码中引用jquery变量(ASP.NET MVC) - Referencing jquery variables in C# code (ASP.NET MVC) ASP.NET MVC 5-@ C#代码在运行时未翻译 - ASP.NET MVC 5 - @C# code not being translated at runtime 从AJAX中的javascript向C#方法发布请求,无返回值,ASP.NET,MVC - Posting request from javascript in AJAX to C# methods, no returning value, ASP.NET, MVC 如何将值从javascript变量传递给asp.net mvc中的c#变量 - how to pass a value from javascript variable to c# variable in asp.net mvc 如何在asp.net mvc中使用razor viewmodel将c#guid值赋给javascript变量? - How to assign c# guid value to javascript variable using razor viewmodel in asp.net mvc? 如何从 asp.net 中的 java 脚本代码(客户端)访问隐藏字段值 c# - How to access hidden field value from java script code(Client side) in asp.net c# 从C#Asp.net后面的代码设置输入文本字段的值 - Set the value of input text field from code behind C# Asp.net 如何在javascript中设置asp.net隐藏字段并访问c#代码后面的值 - how to set asp.net hidden field in javascript and access the value in c# code behind 从JavaScript函数获取值到我的ASP.NET/C#代码隐藏 - Getting a value from a JavaScript function to my ASP.NET / C# Code-Behind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM