如何调用javascript函数从MVC3控制器 [英] How to call javascript function from a controller in MVC3

查看:107
本文介绍了如何调用javascript函数从MVC3控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周,我不能因此找到一个解决方案,我发现自己在这里。从我读过,我会一直能够做到这一点使用RegisterClientScript或的RegisterClientScriptBlock asp.net web表单。我不能任何MVC3文档中找到。
我有以下功能在MVC 3浏览:

MyTest的观点:

 < D​​IV数据角色=内容>< D​​IV ID =mappingTable>< / DIV>< / DIV> < /节>
 @section脚本{
 <脚本类型=文/ JavaScript的>    $(#JQM家)。生活(pageinit',函数(){
        addTableRow(添加数据表< BR />');
    });    //我要到能够调用来自控制器的功能。
    功能addTableRow(MSG){
        $('#mappingTable')追加(MSG)。
    };   < / SCRIPT>
 }

在我的控制器我有以下。

 公共类MyTestController:控制器
    {
      #区域类Constractor       公共MyTestController()
       {
           Common.ControllerResourceEvent + =新System.EventHandler< ResourceEventArgs>(Common_ResourceEvent);
       }    私人无效Common_ResourceEvent(对象发件人,ResourceEventArgs E)
    {
        //我要调用的函数addTableRow并通过ResourceEventArgs
    }    #endregion类Constractor    公众的ActionResult指数()
    {
        返回查看();
    }
}


解决方案

您真的不能从控制器通话客户端的JavaScript。你能做什么,假设你想要做的事类似RegisterClientScript这是JS喷code到您的网页,可以轻松完成pretty。您可以创建一个上有一个字符串属性的模型(这只不过是一个简单的类以上)。属性值设置到要注入的客户端JS code。通过模型到视图。在您看来,引用属性 - 是这样的:

 公共类SampleModel
{
   公共字符串JS {搞定;组; }
}公众的ActionResult指数()
{
    VAR模型=新的SampleModel();
    model.JS =addTableRow('我的信息');;
    返回查看(模型);
}//在视图(在顶部 - 请注意模式和模式的演员
@model Namespace.SampleModel
//然后你的脚本
<脚本类型=文/ JavaScript的>
   @ Model.JS

或者,如果你不希望创建模式,您可以通过ViewBag,这是一个动态的对象传递它:

 公众的ActionResult指数()
{
    ViewBag.JS =addTableRow('我的信息');;
    返回查看();
}//在视图:
<脚本类型=文/ JavaScript的>
   @ ViewBag.JS

I have looked round and I cannot find a solutions hence I find myself here. from what i have read i would been able to do this in asp.net web forms using RegisterClientScript or RegisterClientScriptBlock. I cannot find this in any MVC3 documentation. I have the following function in a MVC 3 View :

MyTest View :

<div data-role="content">

<div id="mappingTable"></div>

</div>

 </section>
 @section Scripts {
 <script type="text/javascript">

    $("#jqm-home").live('pageinit', function () {
        addTableRow(' adding data to table <br/>');
    });

    //I want to the able to call the function from the controller.
    function addTableRow(msg) {
        $('#mappingTable').append(msg);        
    }; 

   </script>
 }

In My Controller I have the following.

public class MyTestController : Controller
    {
      #region Class Constractor

       public MyTestController()
       {            
           Common.ControllerResourceEvent += new System.EventHandler<ResourceEventArgs>(Common_ResourceEvent);
       }

    private void Common_ResourceEvent(object sender, ResourceEventArgs e)
    {
        //I want to call the function addTableRow and pass ResourceEventArgs 
    } 

    #endregion Class Constractor      

    public ActionResult Index()
    {
        return View();
    }
}

解决方案

You can't really "call" the client Javascript from the controller. What you can do, assuming you want to do something analogous to RegisterClientScript which is injecting JS code into your page, can be done pretty easily. You can either create a model (which is nothing more than a simple class) that has a string property on it. Set the property value to the client-side JS code that you want to inject. Pass the model to the view. In your View, reference the property - something like this:

public class SampleModel
{
   public string JS { get; set; }
}

public ActionResult Index()
{
    var model = new SampleModel();
    model.JS = "addTableRow('My Message');";
    return View(model);
}

// In the view (at the top - note the cast of "model" and "Model"
@model Namespace.SampleModel
// Then your script 
<script type="text/javascript">
   @Model.JS

Or, if you don't want to create model, you can pass it via the ViewBag, which is a dynamic object:

public ActionResult Index()
{
    ViewBag.JS = "addTableRow('My Message');";
    return View();
}

// In the view:
<script type="text/javascript">
   @ViewBag.JS

这篇关于如何调用javascript函数从MVC3控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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