Aniebiet Udofia
Aniebiet Udofia

Reputation: 47

The name XXXX does not exist in the current context-Jquery

I have this code

function pickdet(Id) {
        //alert(Id);
        var dd = Id;
        $.ajax({
            cache: false,
            url: "/PVSigns1/GetDetails",
            data: { 'Id' : Id },
            type: "GET",
            dataType: "json",
            success: function (data) {
                //alert('i dey here')
                var row = "";
                $.each(data, function (index, item) {
                    //row += "<tr><td>" + item.VitalSigns.Sign + "</td><td>" + item.SignValue + "</td><td> <input type= 'button'  onclick= location.href = + '<Url.Action("Create", "PVSigns1", new { Id = item.PVSignId }) ' />" + "</td></tr>";
                    row += "<tr><td>" + item.VitalSigns.Sign + "</td><td>" + item.SignValue +
                        "</td><td> <button class='btn btn-xs btn-success' onclick = '@Url.Action("Create","PVSigns1", new { Id = item.PVSignId })'> <i class='fa fa-edit fa-lg'></i> Edit</button></td></tr>";
                });
                $("#pvv").html(row);
            },
            error: function (result) {
                $("#pvv").html('Nothing to show <br /> <p />' + result );
            }
        });
    }

The issue is it points at the at eh item and complains "the name item does not exist in the current context". The error is the occurs after the @Url.Action, others before that are ok.

"</td><td> <button class='btn btn-xs btn-success' onclick = '@Url.Action("Create","PVSigns1", new { Id = item.PVSignId })'> <i class='fa fa-edit fa-lg'></i> Edit</button></td></tr>";

Upvotes: 0

Views: 4017

Answers (3)

Shyju
Shyju

Reputation: 218722

Look at this line,

@Url.Action("Create","PVSigns1", new { Id = item.PVSignId })

But in your code, item is a javascript variable inside your loop. you cannot pass a javascript variable to the Url.Action method as Url.Action code will be executed way before in the server(At that time it does not have any idea about the future ajax call the page is going to make !!!)

The solution is to use another overload of Url.Action to build the base url to the Create action method and append the javascript variable property and use that.

Also, setting the onclick event to a url might not work. You might consider adding a click event to the button and redirect to the url. You may keep the url in html 5 data attribute.

 var baseLink='@Url.Action("Create","PVSigns1")';
 $.each(data, function (index, item) {
    var link=baseLink+item.PVSignId; 
    row += "<tr><td>" + item.VitalSigns.Sign + "</td><td>" + item.SignValue +
       "</td><td> <button class='editBtn' data-url='"+link+"'>
                              <i class='fa fa-edit fa-lg'></i> Edit</button></td></tr>";
 });

and have some jQuery code to listen to the click event on this button and redirect to the url in our data attribute

$(function(){

  $("button.editBtn").click(function(e){
    e.preventDefault();
    window.location.href=$(this).attr("url");
  });

});

Upvotes: 2

Rapha&#235;l Althaus
Rapha&#235;l Althaus

Reputation: 60493

You've got a quote problem, and you try to mix c# with javascript variables.

You could rather try something like

success: function (data) {

       var row = "";
       //the "base url"
       var url = '@Url.Action("Create","PVSigns1")' + '?Id=';
       $.each(data, function (index, item) {
            //add the id value here
            var clickUrl= url +item.PVSignId;
            row += "<tr><td>" + item.VitalSigns.Sign + "</td><td>" + item.SignValue +
                    "</td><td> <button class='btn btn-xs btn-success' onclick = '" + clickUrl +"'> <i class='fa fa-edit fa-lg'></i> Edit</button></td></tr>";
            });
            $("#pvv").html(row);
        },

Upvotes: 1

Marc B
Marc B

Reputation: 360592

JS syntax errors:

"</td><td> <button class='btn btn-xs btn-success' onclick = '@Url.Action("Create","PVSigns1", new { Id = item.PVSignId })'> <i class='fa fa-
^--start string                                                          ^--end string

You cannot embed "bare" quotes of the same type you use to delimit a string, within that string. They must be escaped:

.... @Url.Action(\"Create\", \"PVSigns1\" ...

Upvotes: 0

Related Questions