Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript parameter doesnt exist in the current context

why doesnt this work? how can i declare these? considering topicid will be an int and topicname will be a string.

function changetotopicdetail(topicid, topicname) {
    $('#loadingAjaxs').show();
    $('#flubestext').hide();
    $('#contentwrap').load('@Url.Action("Detail", "Topics", new { @id = topicid,  @namespace = topicname, @Forum = "all", @page = 0})', function () { 
    $('#loadingAjaxs').hide();
   $('#flubestext').show(); 
   window.history.pushState(null, 'title', '/topics');
   })
}

thanks

like image 649
mxadam Avatar asked Feb 16 '23 03:02

mxadam


1 Answers

According to @alxndr tip, this question has the same mistake and it is solved with a code like that:

function changetotopicdetail(topicid, topicname) {
    $('#loadingAjaxs').show(); 
    $('#flubestext').hide();

    var link = '@Url.Action("Detail", "Topics", new { @id = -1,  @namespace = -2, @Forum = "all", @page = 0})';

    link = link.replace("-1", topicid);
    link = link.replace("-2", topicname);

    $('#contentwrap').load(link, function () { 
        $('#loadingAjaxs').hide();
        $('#flubestext').show(); 
        window.history.pushState(null, 'title', '/topics'); 
    });
}

You mistake is that you're trying to access two JavaScript variables(topicid and topicname) in the Razor - view - context. Razor can't access it, so those two variables are unknown for it. What the code is doing is to print the @Url.Action result on a JavaScript variable and then, in JavaScript (where you can access those parameters) replace that link joker chars -1 and -2 with the parameters. So you got the link the way you need it. Hope my explanation is clear.

like image 118
DontVoteMeDown Avatar answered Feb 17 '23 18:02

DontVoteMeDown