如何在视图(MVC3)中处理WCF的JSON输出

发布于 2024-12-07 11:35:25 字数 1697 浏览 0 评论 0原文

我有一个场景,WCF 将以下数据(在下面给出的函数中)返回到 VIEW。

private List<KeyDatesCalendar> GetKeyDatesCalendarData()
    {
        //Dummy Data for BrandsCalendar CheckList
        var keyDatesCalendar = new List<KeyDatesCalendar>()
        {     

            new KeyDatesCalendar()
            { 
                EventText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", 
                EventDate = new DateTime(2011, 02, 09), 
                EventType = 3
            },

            new KeyDatesCalendar()
            { 
                EventText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", 
                EventDate = new DateTime(2011, 03, 05), 
                EventType = 3
            },

            new KeyDatesCalendar()
            { 
                EventText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", 
                EventDate = new DateTime(2011, 03, 06), 
                EventType = 4
            },

};

视图中数据的处理是通过以下代码完成的:

initCalendars({
    from       : '02/01/2011',
    to         : '01/31/2013',
    dates : [
        @for(int i=0, [email protected]; i<l; i++)
        {
            @Html.Raw("['" + @Model.KeyDatesCalendar[i].EventDate.ToString("yyyy/MM/dd") + "'," + @Model.KeyDatesCalendar[i].EventType + ",'" + @Model.KeyDatesCalendar[i].EventText + "']" + (i < (l-1) ? "," : ""));
        }
    ]
});

我如何接收 JSON 输出并在视图中处理相同的值,而不是 WCF 方法中的硬编码值。

我是这里的初学者,感谢您的详细解答。

谢谢, 阿达什

I have a scenario where the WCF returns the follwing data ( in the function given below) to a VIEW.

private List<KeyDatesCalendar> GetKeyDatesCalendarData()
    {
        //Dummy Data for BrandsCalendar CheckList
        var keyDatesCalendar = new List<KeyDatesCalendar>()
        {     

            new KeyDatesCalendar()
            { 
                EventText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", 
                EventDate = new DateTime(2011, 02, 09), 
                EventType = 3
            },

            new KeyDatesCalendar()
            { 
                EventText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", 
                EventDate = new DateTime(2011, 03, 05), 
                EventType = 3
            },

            new KeyDatesCalendar()
            { 
                EventText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", 
                EventDate = new DateTime(2011, 03, 06), 
                EventType = 4
            },

};

The processing of the data in view is done by the following code:

initCalendars({
    from       : '02/01/2011',
    to         : '01/31/2013',
    dates : [
        @for(int i=0, [email protected]; i<l; i++)
        {
            @Html.Raw("['" + @Model.KeyDatesCalendar[i].EventDate.ToString("yyyy/MM/dd") + "'," + @Model.KeyDatesCalendar[i].EventType + ",'" + @Model.KeyDatesCalendar[i].EventText + "']" + (i < (l-1) ? "," : ""));
        }
    ]
});

Instead of the hardcoded values in WCF method, how Do i recieve a JSON output and process the same in View.

I am a beginner here, appreciate your detail answers.

Thanks,
Adarsh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红墙和绿瓦 2024-12-14 11:35:25

我同意之前的许多评论,如果您使用 ASP.NET MVC,您不妨从那里进行 JSON 转换(看看 JsonResult 类)。但是,如果您确实希望 WCF 服务以 JSON 格式返回结果,请参阅博客文章我前一段时间写的可能有帮助。

伊恩

I would agree with many of the previous comments, if you're using ASP.NET MVC you might as well do the JSON conversion from there (have a look at JsonResult class). However, if you really want the WCF service to return the result in JSON format, this blog post I wrote a while back might help.

Iain

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文