How to use “@” symbol in Jscript in Razor syntax o

2019-09-16 23:35发布

I am developing MVC 3 application with Razor syntax.

I am wring a Jscript in the cshtml file.... I I want to use @ in the JScript but it showing yellow backgroud.

I wan to assign the integer value to button ID how to do that ?

here is the code...

  success: function (data) {

                $("p.p12").append

                ("<div style=\"background-color:#FAFAFA\";>Recently Added... <br /><a href=\"../../Employee/Details/" + data.OwnerID + "\">" + data.OwnerName + "</a>"
                + data.cmtDateTime + "  <span class=\"EmpName\"><button type=\"button\" id = \" @item.Id \"class=\"deleteComment\">Delete</button></span> <br />" + data.msg + "</div>");

                document.getElementById('Comment').value = "";
                $(".ParentBlock").slideDown("slow");
                $(".ParentBlock").attr("DataCommentId",data.Id);


                $('.ShowComments').text('Hide Comments');

            }

Here is the image...

enter image description here

What should I do for @ symbol ?

2条回答
Ridiculous、
2楼-- · 2019-09-16 23:45

Are you sure that item exist? (only this word is underlined) What type of error you receive? Pleaase, show more code.

Added:

Working example:

@{ int item = 123; }

Jquery code:

$(function () {
    var name = 'some_name';
    alert('<div class="' + name + '">@item</div>');
});

This works perfect.

查看更多
【Aperson】
3楼-- · 2019-09-17 00:03

Your current code is correct. Run it and you will see that it will work. As far as the yellow squiggle is concerned, well, ignore it and stop trusting automated code editors, trust the code you write - Razor Intellisense is far from perfect. Hopefully they will improve it in future versions.

As an alternative to build this element you could use the following because your code is a hell of a mess (for example you don't have spaces between the attributes, you have inverted the ; and ", ...):

$('p.p12').append(
    $('<div/>', {
        style: 'background-color:#FAFAFA;',
        html: 'Recently Added...'
    })
    .append($('<br/>'))
    .append($('<a/>', {
        href: '@Url.Action("Details", "Employee", new { id = "__employeeId__" })'.replace('__employeeId__', data.OwnerID),
        html: data.OwnerName
    }))
    .append(data.cmtDateTime)
    .append($('<span/>', {
        'class': 'EmpName',
        html: $('<button/>', {
            type: 'button',
            id: '@item.Id',
            'class': 'deleteComment',
            html: 'Delete'
        })
    }))
    .append($('<br/>'))
    .append(data.msg)
);
查看更多
登录 后发表回答