有人有JavaScript ActionResults的示例吗?我在获得返回后执行脚本方面遇到了困难。谢谢。
注:此处的ActionResults是指ASP.NET MVC中的ActionResult对象。有人有JavaScript ActionResults的示例吗?我在获得返回后执行脚本方面遇到了困难。谢谢。
注:此处的ActionResults是指ASP.NET MVC中的ActionResult对象。我在一篇博客文章中找到了一个例子,该例子被称为反模式,因为控制器必须具有深入了解视图的知识才能正常运作。
public ActionResult DoSomething() {
string s = "$('#some-div').html('Updated!');";
return JavaScript(s);
}
<script>
$(document).ready(function () {
$("button").click(function () {
$.getScript("/Home/ShowAlert");
});
});
</script>
<button>Use Ajax to get and then run a JavaScript</button>
在控制器中:
public JavaScriptResult ShowAlert() {
var script = "alert('Hello');";
return new JavaScriptResult() { Script = script };
}
这可能会起作用...
public ActionResult Search(string name)
{
// var someScript = Server.HtmlEncode("<script>alert('Hello')</script>");
return Content("<script>alert('Hello')</script>" );
}