MVC的JavaScriptResult使用
JavaScriptResult的使用有两个必要的前提:
1.Ajax
2.jquery.unobtrusive-ajax.js
使用代码示例
controller
public ActionResult JavaScriptTest()
{
return JavaScript("alert('Controller.JavaScriptResult test');");
}
View
<script src="~/Scripts/jquery-1.11.2.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script> <!--用Ajax生成-->
@Ajax.ActionLink("display", "Display", new AjaxOptions())
@using (Ajax.BeginForm("Display", new AjaxOptions()))
{
}
<!--html代码-->
<a data-ajax="true" href="/Area/SubNode/Display">display</a>
<form action="/Area/SubNode/Display?id=1" data-ajax="true" id="form0" method="post"></form>
仔细看View里面的代码,上面的script引用加上的jquery.unobtrusive-ajax.js,下面data-ajax=true。两者缺一不可,否则返回的网页就不是弹框,而是网页源代码。
成功:

失败:

还有一种失败的表现形式是下载页面文件。失败原因都是上面的两个方面没有写好。
JavaScriptResult里面的string除了可以调用系统的函数以外,还可以执行指定的js。
public ActionResult ServerMethod(int id, string name)
{
string result = "客户端传递过来的id:" + id + ",名字:" + name;
return JavaScript(@"$(""#result"").html(""" + result + @""");");
}
参考资料:http://www.tuicool.com/articles/VJR7Zv
http://mazharkaunain.blogspot.com/2011/02/how-to-use-aspnet-mvc-javascriptresult.html
MVC的JavaScriptResult使用的更多相关文章
- ASP.NET MVC的JavaScriptResult
前段时间,我们有学习<在ASP.NET MVC使用JavaScriptResult>http://www.cnblogs.com/insus/p/3960994.html ,今天我们来加强 ...
- MVC ActionResult -- JavaScriptResult,JsonResult
以下是ActionResult的继承图: 大概的分类: EmptyResult:表示不执行任何操作的结果 ContentResult :返回文本结果 JavaScriptResult:返回结果为Jav ...
- 在ASP.NET MVC使用JavaScriptResult
本例中,我们尝试把javascript程序搬至控制器中去.更好地在控制器编写程序. 首先来看看原来的写法. 在SepController控制器,添加如下操作: public ActionResult ...
- 了解ASP.NET MVC几种ActionResult的本质:JavaScriptResult & JsonResult
在之前的两篇文章(<EmptyResult & ContentResult>和<FileResult>)我们剖析了EmptyResult.ContentResult和F ...
- mvc JavaScriptResult的用法
一.JavaScriptResult在MVC中的定义的代码片段 C# 代码 复制 public class JavaScriptResult : ActionResult { public o ...
- System.Web.Mvc.JavaScriptResult.cs
ylbtech-System.Web.Mvc.JavaScriptResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, P ...
- ASP.NET MVC 中使用JavaScriptResult
在浏览器地址栏输入地址,在页面上想通过脚本弹出一个框,看到Controller下有个JavaScript方法,返回的类型是JavaScriptResult,于是想用这个方法弹出框, public Ac ...
- ASP.NET MVC 中使用JavaScriptResult asp.net mvc 返回 JavaScript asp.mvc 后台返回js
return this.Content("<script>alert('暂无!');window.location.href='/Wap/Index';</script&g ...
- 如何实现MVC ActionResult 返回类型为JavaScriptResult
必需的js引用文件 <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>@Scripts ...
随机推荐
- Latex中如何设置字体颜色(3种方式)
Latex中如何设置字体颜色(三种方式) 1.直接使用定义好的颜色 \usepackage{color} \textcolor{red/blue/green/black/white/cyan/ma ...
- vim的全局替换[zz]&把字符替换成回车
本文出自 http://blog.csdn.net/shuangde800 本文是在学习<使用vi编辑器, Lamb & Robbins编著>时在所记的笔记. 本文内容 ...
- PHP取整,四舍五入取整、向上取整、向下取整、小数截取
PHP取整数函数常用的四种方法: 1.直接取整,舍弃小数,保留整数:intval(): 2.四舍五入取整:round(): 3.向上取整,有小数就加1:ceil(): 4.向下取整:floor(). ...
- Atitit Loading 动画效果
Atitit Loading 动画效果 使用才场景,加载数据,以及显示警告灯.. 要有手动关闭按钮 <div class="spinner loading_part" sty ...
- 【再话FPGA】在xilinx中PCIe IP Core使用方法
采用Xilinx Virtex-5 XC5VSX50T-FF1136 FPGA或者Xilinx Virtex-5 XC5VSX95T-FF1136的板子.采用ISE13.2环境.步骤:一.建立一个IS ...
- scala future
https://docs.scala-lang.org/overviews/core/futures.html https://docs.scala-lang.org/overviews/index. ...
- 关于 Nginx upstream keepalive 的说明
模块是 HttpUpstreamModule,配置的一个例子: [shell]upstream http_backend { server 127.0.0.1:8080; keepalive 1 ...
- [Windows Azure] How to Create and Configure SQL Database
How to Create and Configure SQL Database In this topic, you'll step through logical server creation ...
- 每日英语:Political Gridlock, Beijing Style
To admirers outside the country, China's political system stands far above the dysfunctional democra ...
- 整理Lua和Unity和Lua交互文章链接
重点文章: 1.[Unity3D]Unity3D游戏开发之Lua与游戏的不解之缘(上) 2.[Unity3D]Unity3D游戏开发之Lua与游戏的不解之缘(中) 3.Lua和C++交互详细总结 4. ...