admin管理员组文章数量:1279018
I am sending a filepath ( string) from controller to html page through ViewData and I want to access that string in my javascript, consume it, run some algo in javascript and then use results to consume them and make some graphs on the same html page.
HTML
</head>
<body>
<div id="mychart"></div>
<script>
var path=@ViewData["path"];
//some javascript logic with the string path of the file.
//using results for output chart of id 'mychart'
</script>
</body>
</html>
Controller Action Code:
public IActionResult CellResult(string outputpath)
{
ViewData["path"] = outputPath;
return View();
}
I am sending a filepath ( string) from controller to html page through ViewData and I want to access that string in my javascript, consume it, run some algo in javascript and then use results to consume them and make some graphs on the same html page.
HTML
</head>
<body>
<div id="mychart"></div>
<script>
var path=@ViewData["path"];
//some javascript logic with the string path of the file.
//using results for output chart of id 'mychart'
</script>
</body>
</html>
Controller Action Code:
public IActionResult CellResult(string outputpath)
{
ViewData["path"] = outputPath;
return View();
}
Share
Improve this question
asked Dec 30, 2016 at 14:45
Muhammad TouseefMuhammad Touseef
4,4654 gold badges35 silver badges77 bronze badges
1 Answer
Reset to default 10Since it is a string value, you need to wrap it in either single quotes or double quotes.
var path='@ViewData["path"]';
alert(path);
EDIT : As per the ment
How can I send a list of string from controller to html page and use it in javascript? instead of string i wanna send a list of string
If you want to send a list of string, it is the same, but since it is a plex type now, you need to use the json serializer to convert this object to a string.
So in your server
var list = new List<string> {"Hi", "Hello"};
ViewBag.MyList = list;
and in the view
<script>
var list = @Html.Raw(JsonConvert.SerializeObject(ViewBag.MyList));
console.log(list);
</script>
本文标签: htmlASPNET Core ViewData access in javascriptStack Overflow
版权声明:本文标题:html - ASP.NET Core ViewData access in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741251189a2365820.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论