简体   繁体   English

如何在ASP.NET MVC中的脚本标记内访问某些数据?

[英]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.

相关问题 如何以编程方式创建<script> tag and a script inside an ASP.NET page? - How to programmatically create a <script> tag and a script inside an ASP.NET page? 如何获得ASP.NET MVC包管理器来呈现具有type =“ module”属性的脚本标记? - How to get the ASP.NET MVC bundle manager to render a script tag with the type = “module” attribute? 如何在 ASP.NET Core MVC razor 视图中使用脚本标签 - How to use script tag in an ASP.NET Core MVC razor view 如何创建自定义的Java脚本警报并将其传递给我的asp.net MVC内部的对象属性 - how to create a customized java script alerts and pass it an object property inside my asp.net MVC 我如何将动态日期传递给 asp.net MVC 应用程序中的 Java 脚本 - How i can pass dynamic date to a Java Script inside my asp.net MVC application 如何检查ASP.NET MVC 4中脚本中的许多值? - How do I check many values inside script in ASP.NET MVC 4? 如何在我的asp.net mvc中的脚本文件中获取当前登录用户名 - How to get the current login user name in my Script file inside my asp.net mvc 如何使用来自 model 的数据更新 Javascript 中的 div 标签,用于提交表单 asp.net mvc - How to update div tag in Javascript with data from model for onsubmit form asp.net mvc ASP.Net MVC 将数据从脚本传递到新视图 - ASP.Net MVC passing data from script to new view 如何在asp.net用户控件中动态附加脚本标记? - How to append script tag dynamically in asp.net user control?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM