如何访问的ViewData的JavaScript [英] How to Access ViewData in javascript

查看:115
本文介绍了如何访问的ViewData的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有通过JavaScript访问的ViewData对象的问题。

I am having a problem accessing the ViewData object through javascript.

我已经设置了ViewData的对象控制器和视图我试图看到相同的ViewData对象,像这样的内容的document.ready事件:

I have set the ViewData object on the controller and on document.ready event of the view i am trying to see the contents of that same ViewData object like so:

     var test = <%= ViewData["NAME"].ToString() %>;
     alert(test);



我这句话将运行后没有得到在此之后的警告消息,没有我的剧本。我假设这个脚本无效因而杀害一切之后。我试图在同一个脚本的几个不同的版本没有任何运气。

I don't get an alert message after this and none of my script after this statement will run. I am assuming this script is invalid thus killing everything afterwards. I have tried a few different variations of this same script without any luck.

我缺少的是在这里吗?

在此先感谢,
比利

Thanks in advance, Billy

推荐答案

尝试添加围绕输出一些报价:

Try adding some quotes around the output:

var test = '<%= ViewData["NAME"].ToString() %>';
alert(test);



编辑:



我注意到你再使用名称为重点;可这个名字永远有它的一个单引号?如果它可能是任何值永远不会包含一个,你会想这样的事情,而不是让你的网页不会再次打破(虽然在技术上这似乎是更多的控制器或模型作业):

I notice you're using NAME for the key; could this name ever have a single quote in it? If it's possible that any value will ever contain one, you will want something like this instead so your page doesn't break again (although technically this seems to be more of a job for the controller or model):

var test = '<%= ViewData["NAME"].ToString().Replace("'", "\\'") %>';
alert(test);

这篇关于如何访问的ViewData的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆