4

私は周りを見回しましたが、解決策が見つからないため、ここにいます。私が読んだことから、RegisterClientScriptまたはRegisterClientScriptBlockを使用してasp.net Webフォームでこれを行うことができました。これはどの MVC3 ドキュメントにもありません。MVC 3 View に次の機能があります。

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

3 に答える 3

10

コントローラーからクライアント 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
于 2012-04-15T22:31:54.560 に答える
2

JavaScriptModel ( http://jsm.codeplex.com ) を使用すると、次の方法で実行できます。

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

関数を使用してjsファイルを作成し、Tables asリストをjs変数に追加するとさらに良いでしょう。次に、js 関数がリストを反復処理し、テーブルを追加します。

public ActionResult Index()
{
    this.AddJavaScriptVariable("TableListInJavaScript", tableList);
    this.AddJavaScriptFunction("MyTableReadyFunction");
    return View();
}
于 2013-01-08T12:01:45.273 に答える
0

ここであなたは間違った角度から来ているのではないかと思います。コントローラーから直接ページで JavaScript を呼び出すことはできません。つまり、javascript から ajax を使用してコントローラー メソッドを呼び出すことができます。これを行うのに役立つ jquery ajax 関数がいくつかあります。最も便利なものは $.post() です

私が最も頻繁に使用するパターンは次のとおりです。

ウェブページで:

$.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 と部分ビューを使用してデータを渡す方法の例であり、役立つと思います。

于 2012-04-15T23:07:18.617 に答える