[英]How to access some data inside script tag in ASP.NET MVC?
I'm trying to initialize a jQuery call with some parameters, but I don't know how to access them. 我正在尝试使用一些参数来初始化jQuery调用,但是我不知道如何访问它们。
Now I have: 我现在有:
// Controller code
public ActionResult Offer()
{
...
ViewData["max"] = max;
ViewData["min"] = min;
...
return View(paginatedOffers);
}
// View Code
script type="text/javascript">
$().ready(
function() {
// Slider
$('#slider').slider({
min: %= Html.Encode(ViewData["min"]) %>,
max: %= Html.Encode(ViewData["max"]) %>
});
});
/script>
But I noticed that I don't have access to ViewData inside the script tag. 但是我注意到我没有访问script标记内的ViewData的权限。
Is there a mistake on my side? 我这边有错吗? Can you point me in the right direction, please?
你能指出我正确的方向吗?
(I'm new to ASP/C#). (我是ASP / C#的新手)。
Thank you, M. 谢谢你
Edits: Start of script tag and ASP intentionally left out. 编辑:脚本标记的开始和ASP故意遗漏了。
As Mike Chaliy pointed out, it works, but you don't get intellisence. 正如Mike Chaliy指出的那样,它可以奏效,但是您不会有智慧。 Because of a bug in my script, I thought that it doesn't work at all.
由于脚本中的错误,我认为它根本不起作用。
Thanks Mike (and CMS too). 谢谢Mike(还有CMS)。
You are missing some characters, the beginning of the <%%>
tags, and you will need a comma to separate the min and max options: 您缺少一些字符,即
<%%>
标记的开头,并且需要用逗号分隔最小和最大选项:
<script type="text/javascript">
$(function() {
// Slider
$('#slider').slider({
min: <%= Html.Encode(ViewData["min"]) %>,
max: <%= Html.Encode(ViewData["max"]) %>
});
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.