0

I've got my little asp.net mvc app working as expected, however I'm building the JSON incorrectly.

Here's my current way of building a JSON object for consumption within Sencha

        // note: this is using a RAZOR / C# foreach loop to build a JSON string
        var videosToShow = [
        @foreach (Web.Models.VideoListViewModel video in Model){
            @Html.Raw("{ id: " + @video.id + ",")
            @Html.Raw(" title: \"" + @video.title + "\" },")
        }
        ];

Then I have a Sencha Template

        videoTpl = new Ext.XTemplate([
            '<tpl for=".">',
            '<div>',
            '<iframe src="http://player.vimeo.com/video/{id}?title=0&amp;byline=0&amp;portrait=0&amp;color=80ceff&amp;fullscreen=1" ',
            'width="' + screenWidth + '" ',
            'height="' + screenHeight + '" ',
            'frameborder="0">',
            '</iframe>',
            '&nbsp;&nbsp;{title}',
            '</div>',
            '</tpl>'
        ]);

as well as a video panel

        videoPanel = new Ext.Panel({
            title: "Videos",
            tpl: videoTpl,
            iconCls: "tv",
            dockedItems: [{ xtype: "toolbar", title: "Videos"}],
            scroll: "vertical"
        });

and my root TabPanel

        rootPanel = new Ext.TabPanel({
            fullscreen: true,
            layout: 'card',
            region: 'center',
            items: [videoPanel, biblePanel, aboutPanel, helpPanel, morePanel],
            tabBar: { dock: 'bottom' }
        });

        videoPanel.update(videosToShow);

The bottom line here is that the above works just fine. I just don't like the internal JSON string, I'd much rather send the JSON from a URL.

It's probably something simple that I'm missing, but any influence will be greatly appreciated.

4

1 回答 1

0

像这样做:

var videosToShow = @Html.Raw(Json.Encode(Model));

或者当然,如果您的模型具有许多属性并且您只想要id并且title

var videosToShow = @Html.Raw(Json.Encode(Model.Select(x => new { x.id, x.title })));

它会更好/更容易/更短/更安全/避免意大利面条代码/没有循环/工作。

在评论中找到编辑答案。

于 2011-06-02T17:24:26.607 回答