惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Palo Alto Networks Blog
大猫的无限游戏
大猫的无限游戏
Martin Fowler
Martin Fowler
GbyAI
GbyAI
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
量子位
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
CERT Recently Published Vulnerability Notes
Recent Announcements
Recent Announcements
A
About on SuperTechFans
aimingoo的专栏
aimingoo的专栏
P
Privacy International News Feed
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
博客园 - 叶小钗
L
Lohrmann on Cybersecurity
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
P
Proofpoint News Feed
NISL@THU
NISL@THU
博客园 - Franky
C
Cybersecurity and Infrastructure Security Agency CISA
The Register - Security
The Register - Security
M
MIT News - Artificial intelligence
Know Your Adversary
Know Your Adversary
A
Arctic Wolf
F
Full Disclosure
T
Threat Research - Cisco Blogs
P
Privacy & Cybersecurity Law Blog
The Hacker News
The Hacker News
博客园 - 【当耐特】
D
Docker
T
Tailwind CSS Blog
S
SegmentFault 最新的问题
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Jina AI
Jina AI
Help Net Security
Help Net Security
V
Visual Studio Blog
小众软件
小众软件
B
Blog
Vercel News
Vercel News
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
Forbes - Security
Forbes - Security
N
Netflix TechBlog - Medium
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
C
Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic

博客园 - Kiven

50个常用SQL语句 [摘]在ASP.NET MVC中使用DropDownList SQL Server里面可能经常会用到的日期格式转换方法 « 给初学C++者的50条忠告 C++/CLI学习入门数组 asp.net页面刷新后样式就发生了改变 System::String^ / std::string / char * 间的转换(部分) [摘]C/C++实现js的split函数功能 char*与System::String^的相互转换 Windows Phone 7 常用控件之Map Windows Phone 7 常用绘图控件 [网摘]深入浅出解读微软云计算:让云触手可及 【网摘日记】云计算五层架构 [MVP在线交流]微软原型设计工具SketchFlow企业级应用(二) Web项目VS2005转VS2008平台注意事宜。 [俱乐部在线交流]微软原型设计工具SketchFlow企业级应用(一) [武汉站]Windows 7社区发布活动圆满成功! [武汉站]Windows 7 社区发布活动 宏博软件“移动公教站”之“微软新技术预览”系列资料
[摘]Asp.net MVC 3中Session与ViewBag传值到Js中
Kiven · 2013-03-31 · via 博客园 - Kiven

Asp.net MVC 3 中Session与ViewBag传值到Javascript中, 主要方法有:
1. 使用Javascript Variable  
假设有say _layout.cshtml
<head>
    ...
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    @RenderSection("my_script_variables", false)
    <script src="@Url.Content("~/Scripts/external.js")" type="text/javascript"></script>
    ...
</head>


增加到你的View:

@Section my_script_variables {
<script type="text/javascript"> 
  var variable1 = '@myVar1', variable2 = '@Session["myVar2"]', variable3 = '@ViewBag.myVar3';  
 </script>
}
 
假设你有这样的Value:
@{    String myVar1 = "First";    Session["myVar2"] = "Second";    ViewBag.myVar3 = "Third"; }
 
在外部的Js文件中,你到得来First Second Third的alert消息。
alert(variable1 + ' ' +variable2 +' '+ variable3);
 
2.使用Controller的特性
可以放一个参数到你的控件
<input type="hidden" value="@Session["myVar2"]" id="myHiddenVar" />

然后
alert($('#myHiddenVar').val());
也可以使用Data特性:
<a id="myLink" data-variable1="@myVar1" data-variable2="@Session["myVar2"]" data-variable3="@ViewBag.myVar3">
    Test</a>
 
然后在外面引用的JS中:
$('#myLink').click(function () {
 alert($(this).data('variable1')+' ' +$(this).data('variable2')+'
'+$(this).data('variable3')); }
);
我们得到同样的结果
 
3.RazorJS
 
  你可以从NuGet安装它.  它允许你写Razor风格的C#代码在Js文件中。
View:
@{    
    String var1 = "First"; Session["var2"] = "Second";
       ViewBag.var3 = "Third";
       Dictionary<string, string> test1 = new Dictionary<string, string>();
       test1.Add("var1", var1);
       test1.Add("var2", Session["var2"].ToString());
       test1.Add("var3", ViewBag.var3);   
  }
@Html.RazorJSInline("~/Scripts/external.js", test1);
 
然后在外部的external.Js中:
@{
    var myobj = (Dictionary<string, string>)Model;
 }
alert('@myobj["var1"]' +' '+ '@myobj["var2"]'+' ' +'@myobj["var3"]');

 
最后还是同样的结果。
希望对你Web开发有帮助。


作者:Petter Liu

posted @ 2013-03-31 13:12  Kiven  阅读(2236)  评论()    收藏  举报