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使用的更多相关文章

  1. ASP.NET MVC的JavaScriptResult

    前段时间,我们有学习<在ASP.NET MVC使用JavaScriptResult>http://www.cnblogs.com/insus/p/3960994.html ,今天我们来加强 ...

  2. MVC ActionResult -- JavaScriptResult,JsonResult

    以下是ActionResult的继承图: 大概的分类: EmptyResult:表示不执行任何操作的结果 ContentResult :返回文本结果 JavaScriptResult:返回结果为Jav ...

  3. 在ASP.NET MVC使用JavaScriptResult

    本例中,我们尝试把javascript程序搬至控制器中去.更好地在控制器编写程序. 首先来看看原来的写法. 在SepController控制器,添加如下操作: public ActionResult ...

  4. 了解ASP.NET MVC几种ActionResult的本质:JavaScriptResult & JsonResult

    在之前的两篇文章(<EmptyResult & ContentResult>和<FileResult>)我们剖析了EmptyResult.ContentResult和F ...

  5. mvc JavaScriptResult的用法

    一.JavaScriptResult在MVC中的定义的代码片段   C# 代码   复制 public class JavaScriptResult : ActionResult { public o ...

  6. System.Web.Mvc.JavaScriptResult.cs

    ylbtech-System.Web.Mvc.JavaScriptResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, P ...

  7. ASP.NET MVC 中使用JavaScriptResult

    在浏览器地址栏输入地址,在页面上想通过脚本弹出一个框,看到Controller下有个JavaScript方法,返回的类型是JavaScriptResult,于是想用这个方法弹出框, public Ac ...

  8. ASP.NET MVC 中使用JavaScriptResult asp.net mvc 返回 JavaScript asp.mvc 后台返回js

    return this.Content("<script>alert('暂无!');window.location.href='/Wap/Index';</script&g ...

  9. 如何实现MVC ActionResult 返回类型为JavaScriptResult

    必需的js引用文件 <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>@Scripts ...

随机推荐

  1. nginx Server names

    通配符名称 正則表達式名称 混合名称 优化 兼容性 server名称定义使用的server_name指令和决定哪个server块用于一个给定的请求. 參见"怎样Nginx处理一个请求&quo ...

  2. alibaba fastjson TypeReference 通过字符串反射返回对象

    TypeReferenceEditNew Page温绍 edited this page Nov 3, 2017 · 8 revisions1. 基础使用在fastjson中提供了一个用于处理泛型反序 ...

  3. jmeter 正则获取参数集合和ForEach控制器结合使用

    怎么把第一个请求获取的返回的多个id,在第二个请求中逐个以单个id作为请求参数来请求? 为了解决这个问题,模拟下该场景 1.请求www.163.com 主页,获取响应中的所有数字,这个获取的数字集合暂 ...

  4. django -- 联合索引

    一.定义: from django.db import models # Create your models here. class Person(models.Model): first_name ...

  5. 精确光源(Punctual Light Sources)

    <Physically-Based Shading Models in Film and Game Production>(SIGGRAPH 2010 Course Notes) (地址: ...

  6. 怎么运行 ASP.NET Core控制台程序

    aspnet test.dll

  7. Atitit 图像处理类库安装与安装模式的前世今生与未来大趋势attilax总结.docx

    Atitit 图像处理类库安装与安装模式的前世今生与未来大趋势attilax总结.docx 1. 安装的原理,主要是解压,复制,设置三大步1 2. 目前我们常见的三大种安装模式,二进制模式与源码安装模 ...

  8. Xcode6中添加pch文件

    转自:http://www.cnblogs.com/YouXianMing/p/3989155.html 1. 新建工程: 2. 创建pch文件: 3. 在setting里面进行设置: 4. 一切尽在 ...

  9. log_output参数是指定日志的存储方式

    log_output='FILE'表示将日志存入文件,默认值是'FILE' log_output='TABLE'表示将日志存入数据库,这样日志信息就会被写入到mysql.slow_log表中. mys ...

  10. 转axios 的应用

    尤雨溪之前在微博发布消息,不再继续维护vue-resource,并推荐大家开始使用 axios . 一.axios 简介 axios 是一个基于Promise 用于浏览器和 nodejs 的 HTTP ...