2012-04-15 68 views
3

私は見た目が丸いので解決策を見つけることができません。私が読んだことから、私はasp.net WebフォームでこれをRegisterClientScriptまたはRegisterClientScriptBlockを使って行うことができました。私はMVC3のドキュメントでこれを見つけることができません。私は次のことを持っている私のコントローラでMVC3のコントローラからjavascript関数を呼び出す方法

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

のMyTestビュー: 私はMVC 3ビューで、以下の機能を持っています。

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

+0

あなたは何をしようとしていますか?ボタンのクリックでサーバーを呼び出すと、クライアントが呼び出されます。なぜAJAXを使ってサーバを呼び出すのではなく、コールバックであなたの関数を呼び出すことができます。 – RPM1984

答えて

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

0

私はあなたが間違った角度で物事から来ているかもしれないと思います。あなたはコントローラから直接ページ上でjavascriptを呼び出すことはできません。つまり、javascriptからコントローラメソッドを呼び出し、ajaxを使用して、これを行うためのいくつかのjquery ajax関数があります。 Webページで

:コントローラで

$.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); 
} 

で最も有用な一つであるの$ .post()は

私が最も頻繁に使用するパターン以下であります部分図:

@model string 
<p> Model </p> 

これは、ページの文字列をコントローラに戻し、パーシャルビューに戻してページに戻します。これはまさにあなたがやりたいことではないかもしれませんが、Ajaxを使用してデータを渡す方法の例と、あなたに役立つと思われる部分的なビューです。あなたはそれを次のように行うことができますJavaScriptModel(http://jsm.codeplex.com)で

2

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(); 
} 
関連する問題