简体   繁体   English

如何在.net MVC5控制器中编写JavaScript

[英]How to write JavaScript in .net MVC5 controller

I am new with MVC so forgive my ignorance. 我是MVC的新手,所以请原谅我的无知。 What I have is a Google maps script, which will dynamically change the marker location based on user page. 我所拥有的是一个Google Maps脚本,它将根据用户页面动态更改标记位置。 In web forms I can write the script in code behind like so: 在Web表单中,我可以用下面的代码编写脚本,如下所示:

protected void maps_wrapper_Init(object sender, EventArgs e)
    {

        maps_wrapper.Text = "<script> " +
        "function initialize() {" +
          "var mapOptions = {" +
            "zoom: 12," +
            "center: new google.maps.LatLng(-25.363882, 131.044922)," +
            "mapTypeId: google.maps.MapTypeId.SATELLITE" +
          "};" +

          "var map = new google.maps.Map(document.getElementById('map-canvas')," +
             " mapOptions);" +

          "var image = 'img/mapMarker.png';" +
          "var marker = new google.maps.Marker({" +
            "position: map.getCenter()," +
            "map: map," +
            "title: 'Click to zoom'," +
            "icon: image" +
          "}); " +

          "google.maps.event.addListener(map, 'center_changed', function() {" +
            "window.setTimeout(function() {" +
              "map.panTo(marker.getPosition());" +
            "}, 3000);" +
          "});" +

          "google.maps.event.addListener(marker, 'click', function() {" +
            "map.setZoom(18);" +
            "map.setCenter(marker.getPosition());" +
          "});" +
        "}" +

        "google.maps.event.addDomListener(window, 'load', initialize);" +
        "</script> ";


    }

and call it with a label 并用标签称呼它

<asp:Label runat="server" ID="map_wrapper" OnInit="search_wrapper_Init" />

How would you do this equivalent in MVC? 您将如何在MVC中做到这一点?

Or am I heading in completely the wrong direction? 还是我朝着完全错误的方向前进?

You don't put the script in your controller. 您无需将脚本放入控制器中。 JavaScript is client-side code, you put it in your view. JavaScript是客户端代码,您将其放在视图中。

<script type="text/javascript">
    function initialize() {
      var mapOptions = {
        zoom: 12,
        center: new google.maps.LatLng(-25.363882, 131.044922),
        mapTypeId: google.maps.MapTypeId.SATELLITE
      };

    // etc.

</script>

Or perhaps in its own .js file referenced by your view, or included in a script bundle which the view uses. 或在视图引用的自己的.js文件中,或包含在视图使用的脚本包中。

(It really shouldn't have been in the code-behind in Web Forms, it should have been on the page. Or in a .js file referenced by the page.) (它实际上不应该在Web窗体的代码隐藏中,它应该在页面上。或者在页面引用的.js文件中。)

Actually, this is a valid question when you want to create dynamic Javascripts and send to client side. 实际上,当您要创建动态Javascript并将其发送到客户端时,这是一个有效的问题。 For instance, when you want to pass on your ViewModel-specific data to client side without editing each view for ViewModel-specific data. 例如,当您希望将特定于ViewModel的数据传递给客户端时,无需为特定于ViewModel的数据编辑每个视图。

There are several ways to do this. 有几种方法可以做到这一点。 Here is the easiest way. 这是最简单的方法。 Using ViewBag's dynamic property and add your javascript on the server side and retrieve it on the client side (or your view file). 使用ViewBag的dynamic属性,然后在服务器端添加您的JavaScript,然后在客户端(或您的视图文件)中检索它。 The following is the code: 以下是代码:

In your Controller method, set: 在您的Controller方法中,设置:

Viewbag.clientside_js="<script type=\"text/javascript\"> Your Javascript here </script>";

On your View file, insert the following line: 在您的View文件上,插入以下行:

@Html.Raw(ViewBag.clientside_js)

That's it. 而已。 Of course, you can achieve the same by using JavaScriptSerializer().Serialize() as well. 当然,您也可以通过使用JavaScriptSerializer().Serialize()实现相同的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM