2012-04-15 77 views
3

我已经四处查看,找不到解决方案,因此我在此找到自己的解决方案。从我读过的东西,我将能够使用RegisterClientScript或RegisterClientScriptBlock在asp.net web窗体中执行此操作。我无法在任何MVC3文档中找到它。 我有一个MVC 3查看以下功能:如何从MVC3中的控制器调用javascript函数

MyTest的观点:

<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> 
} 

在我的控制器我有以下。

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(); 
    } 
} 
+0

你想让客户端调用服务器,反之亦然? – McGarnagle 2012-04-15 21:29:34

+0

你想做什么?通过单击按钮调用服务器,然后调用客户端?为什么不只是使用AJAX调用服务器,然后在回调中可以调用你的函数。 – RPM1984 2012-04-15 21:32:43

回答

9

你不能从控制器“调用”客户端Javascript。你可以做什么,假设你想做一些类似于将JS代码注入页面的RegisterClientScript的事情,可以很容易地完成。你可以创建一个模型(它不过是一个简单的类),它有一个字符串属性。将属性值设置为您要注入的客户端JS代码。将模型传递给视图。在你看来,引用属性 - 是这样的:

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 

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

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

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

'@ ViewBag.JS'可能会导致语法错误。如果是这种情况,请尝试'@ Html.Raw(ViewBag.JS)' – oHoodie 2015-04-13 09:59:30

0

我想你可能会从错误的角度来看这里。您无法直接从控制器调用页面上的JavaScript。你只能以其他方式做事情,即使用ajax调用javascript中的控制器方法,有几个jQuery ajax函数可以帮助你做到这一点。最有用的一个是$。员额()

我最常使用的模式如下:

在网页

$.post('TestController/TestGetPartialView','{param1:"string to add"}', function (data) { 

    ('#mappingTable').append(data); // this adds the string to the mapping table. 

},'html'); 
在控制器

[HttpPost] 
public PartialViewResult TestGetPartialView(string param1) 
{ 
    return PartialView("TestPartial", param1); 
} 

在局部视图:

@model string 
<p> Model </p> 

这会将字符串从页面传递回控制器,传递到局部视图然后返回页面。这可能不是你想要做的,但它是一个如何使用ajax和部分视图传递数据的例子,我认为这些数据可以帮助你。

2

随着JavaScriptModel(http://jsm.codeplex.com),你可以做以下的方法:

public ActionResult Index() 
{ 
    this.AddJavaScriptFunction("addTableRow", "My Message"); 
    return View(); 
} 

如果创建一个js文件与函数和列表中添加表到一个js变量,它甚至会更好。然后js函数将迭代列表并添加表。

public ActionResult Index() 
{ 
    this.AddJavaScriptVariable("TableListInJavaScript", tableList); 
    this.AddJavaScriptFunction("MyTableReadyFunction"); 
    return View(); 
} 
相关问题