user1668543
user1668543

Reputation: 309

How to use "@" symbol in Jscript in Razor syntax of MVC ?

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 ?

Upvotes: 0

Views: 310

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038800

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

Upvotes: 2

webdeveloper
webdeveloper

Reputation: 17288

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.

Upvotes: 1

Related Questions