简体   繁体   English

ASP.net Razor MVC5在我的cshtml页面中的javascript中插入@ {}

[英]ASP.net Razor MVC5 insert @{} inside javascript within my cshtml page

new to razor so here is my question. 剃刀新手,所以这是我的问题。

I am wanting to add some dynamic code to my JavaScript created button: 我想向我的JavaScript创建的按钮添加一些动态代码:

$('<button ' +
    '"class="btn btn-default btn-outline" ' +
    'id="addEvent" ' +
    'style="top: -20px; position: relative;" ' +
    'data-tooltips="' + @Html.Raw(new ETTData.Models.Held.tips()._tips["RequestID"]) + '">' +
    'Add New Event' +
 '</button>').insertBefore('#theDT_paginate .pagination');

As you can see above, the data-tooltips is the area where I am trying to insert razor code. 正如您在上面看到的那样, 数据工具提示是我尝试插入剃刀代码的区域。

Though it seems not to be working. 虽然似乎没有用。 It's just displaying like this: 就像这样显示:

$('<button ' +
    '"class="tips btn btn-default btn-outline" ' +
    'id="addEvent" ' +
    'style="top: -20px; position: relative;" ' +
    'data-tooltips="' + example 1 + '">' +
    'Add New Event' +
  '</button>').insertBefore('#theDT_paginate .pagination');

And giving the error of: 并给出以下错误:

Expected ')' 预期为')'

I've tried the example that I found HERE but that does not seem to work? 我已经尝试过在这里找到的示例,但这似乎不起作用?

Since you are embedding the C#/Razor code into the javascript, you really don't need to use the concat for that part since JS doesn't know the differece. 由于您将C#/ Razor代码嵌入到javascript中,因此您真的不需要为此部分使用concat,因为JS不知道其区别。 Try 尝试

'data-tooltips="@Html.Raw(new ETTData.Models.Held.tips()._tips["RequestID"])">' +

Which should result in: 这应导致:

'data-tooltips="example 1' +

That said, your @Html.Raw output is "example 1". 就是说,您的@ Html.Raw输出为“示例1”。 Do you really want that space in there? 您真的想要那里的空间吗?

Unless "example1" is a JS variable defined previously, then you do want the concat but you want to get rid of that space. 除非“ example1”是先前定义的JS变量,否则您确实需要concat,但希望摆脱该空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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