2012-04-15 75 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(); 
} 
相關問題