简体   繁体   中英

Pass a variable from razor to javascript and display it

I have a variable

var result = client.DownloadString(query);

in mvc 4 rzaor. By hovering it in the debugging process, it likes the image below 结果

What I want is to display it, so I return it to javascript with the code:

function myFunction() {
        $('#response').text('@(Url.Action("result"))');
    }

EDIT:

<div id="response"></div>
@{
   var strSearch = "test";
   var options = "include-passage-references=true";
   var client = new WebClient();
   var query = string.Format("http://www.xxx.org/v2/rest/passageQuery?key={0}&passage={1}&options={2}", "IP", Server.UrlEncode(strSearch), options);
   var result = client.DownloadString(query);
 }

However nothing found.

You have to use the ViewBag for that.

On C#:

ViewBag.result = client.DownloadString(query);

On HTML:

function myFunction() {
        $('response').text('@ViewBag.result');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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