如何在单独的脚本中定义的Razor中使用对象

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在单独的脚本中定义的Razor中使用对象相关的知识,希望对你有一定的参考价值。

我正在尝试使用Razor作为我的一种观点,但是我很难弄清一些事情。

@
if (Model != null)

    <script async src="https://securepubads.g.doubleclick.net/tag/js/gpt.js"></script>
    <script>
           var bbbGoogleAdsData = @html.Raw(Model.SerializeToString());
    </script>
    <script>
        window.googletag = window.googletag ||  cmd: [] ;
    googletag.cmd.push(function () 
        googletag.defineSlot('/21848388897/Development/Geo-Landing', [250, 250], [300, 300], "div-id-for-squaretile1")
            .addService(googletag.pubads())
            .setTargeting("pos", "1");
        googletag.pubads().enableSingleRequest();
        googletag.pubads().collapseEmptyDivs();
        googletag.pubads()
            .setTargeting('cityState', bbbGoogleAdsData.param.cityState)
            .setTargeting('country', bbbGoogleAdsData.param.country)
            .setTargeting('countyState', bbbGoogleAdsData.param.countyState)
            .setTargeting('orgType', bbbGoogleAdsData.param.orgType)
            .setTargeting('cleanSearchText', bbbGoogleAdsData.param.cleanSearchText)
            .setTargeting('culture', bbbGoogleAdsData.param.culture)
            .setTargeting('postalCode', bbbGoogleAdsData.param.postalCode)
            .setTargeting('channel',bbbGoogleAdsData.param.channel)
            .setTargeting('tobs', bbbGoogleAdsData.param.tob)
            .setTargeting('refURL',bbbGoogleAdsData.param.refUrl)
            .setTargeting('state', bbbGoogleAdsData.param.state);
        googletag.enableServices();
    );
    </script>

控制台输出:

<script>
               var bbbGoogleAdsData = "param":"cleanSearchText":[],"countyState":["suffolk-ma"],"orgType":[],"culture":["en-us"],"state":["ma"],"cityState":["boston-ma"],"country":["usa"],"postalCode":["02228"],"channel":["0021"],"tobs":[],"refUrl":[],"adSlots":["path":"Geo-Landing","size":null,"id":"div-id-for-squaretile1","position":1];
        </script>
<script>
            window.googletag = window.googletag ||  cmd: [] ;
        googletag.cmd.push(function () 
            googletag.defineSlot('/21848388897/Development/Geo-Landing', [250, 250], [300, 300], "div-id-for-squaretile1")
                .addService(googletag.pubads())
                .setTargeting("pos", "1");
            googletag.pubads().enableSingleRequest();
            googletag.pubads().collapseEmptyDivs();
            googletag.pubads()
                .setTargeting('cityState', bbbGoogleAdsData.param.cityState)
                .setTargeting('country', bbbGoogleAdsData.param.country)
                .setTargeting('countyState', bbbGoogleAdsData.param.countyState)
                .setTargeting('orgType', bbbGoogleAdsData.param.orgType)
                .setTargeting('cleanSearchText', bbbGoogleAdsData.param.cleanSearchText)
                .setTargeting('culture', bbbGoogleAdsData.param.culture)
                .setTargeting('postalCode', bbbGoogleAdsData.param.postalCode)
                .setTargeting('channel',bbbGoogleAdsData.param.channel)
                .setTargeting('tobs', bbbGoogleAdsData.param.tob)
                .setTargeting('refURL',bbbGoogleAdsData.param.refUrl)
                .setTargeting('state', bbbGoogleAdsData.param.state);
            googletag.enableServices();
        );
        </script>

bbbGoogleAdsData是类似的对象:

enter image description here

如何使用这些值?我的输出只是文本bbbGoogleAdsData.param ...

我犯语法错误吗?

答案
这就是我设法做到的:

以上是关于如何在单独的脚本中定义的Razor中使用对象的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Razor 页面中实现两个具有单独 BindProperties 的表单?

如何将对象属性作为参数传递给 Razor 组件

如何(在 Razor 中)将数据从 c# 数组传输到脚本(JS)数组?

在 Razor 中,如何根据元素的属性检查 ViewBag IEnumerable 对象中是不是存在对象?

如何在 Razor .cshtml 和 Html 文件之间共享 JS 变量值(同一解决方案中的 Html 解决方案)

如何从 JavaScript 中的 Razor 模型对象获取 JSON 对象