使用push json数据和显示表</object>填充list <object>

时间:2012-06-18 19:24:29

标签: c# html asp.net-mvc json

我使用aspx引擎。我一直在尝试将数据推送到我的列表中,但我不知道如何。每次我运行该程序时都会创建一个对象的新实例。我不太确定他们在谈论什么..但我确实创建了一个类的实例但仍然无法工作任何人都可以帮助我

这是我试图在我的家庭控制器中填充数据

    public ActionResult GetEvent()
    {
        try
        {

            string i = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
            string et = "KI2XfwQNByLPFdK4i3a74slLT7sjjzYRi9RR7zEtCoQ%3D";
            string t = "20111128183020";
            string _checkingUrl =  String.Format("http://172.22.22.10/SampleAPI/Event/GetEvents?at={0}&et={1}&t={2}&responseFormat=json", i, et, t);
            System.Net.HttpWebRequest request=System.Net.WebRequest.Create(_checkingUrl) as System.Net.HttpWebRequest;
            System.Net.HttpWebResponse response=request.GetResponse() as System.Net.HttpWebResponse;
            System.IO.StreamReader _readResponse=new System.IO.StreamReader(response.GetResponseStream());
            //The encrypted dynamics response in either xml or json
            var _responseAsString=_readResponse.ReadToEnd();
            JavaScriptSerializer parseResponse = new JavaScriptSerializer();

            List<Event> events = parseResponse.Deserialize<List<Event>>(_responseAsString);

            Event eventOjb = new Event();
            events.Add(eventOjb);

            ViewBag.eventss = events;

            _readResponse.Close();
            // check json object
            return Content(_responseAsString.ToString());  



        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
            //log error
        }
        return View();
    }

这是我在模型文件夹

中的Event类
  using System;
  using System.Collections.Generic;
 using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;


 namespace StopMalaria.Models
 {
public class Event
{
    public string event_key { get; set; }
    public string user_token { get; set; }
    public string event_set_key { get; set; }
    public string event_type { get; set; }
    public string event_date { get; set; }
    public string event_amount { get; set; }
    public string event_location_key { get; set; }
    public string event_location_name { get; set; }
    public string event_location_city { get; set; }
    public string event_location_state { get; set; }
    public string event_location_country { get; set; }
    public string event_acknowledged { get; set; }
}

}

这是我的html尝试逐步浏览列表并创建一个表,并在foreach循环上显示错误消息,该对象引用未设置为对象的实例

 <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    GetEvent
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">


   <table id="eventist" border="0" cellspacing="0" cellpadding="0">
    <thead>
        <tr>
            <th>
                event_key
            </th>
             <th>
                user_token
            </th>                
            <th>
                event_set_key
            </th>           
            <th>
                event_type
            </th>
             <th>
              event_date
            </th>
              <th>
                event_amount
            </th>
             <th>
                event_location_key
            </th>                
            <th>
                event_location_name
            </th>           
            <th>
                event_location_city
            </th>
             <th>
              event_location_state
            </th>
            <th>
                event_location_country
            </th>
             <th>
              event_acknowledged
            </th>
        </tr>
    </thead>
    <tbody>
    <%List<StopMalaria.Models.Event> events = new List<StopMalaria.Models.Event>();%>
    <%events = ViewBag.eventss;%>

    <% foreach (var item in events )
       { %>

        <tr>
            <td>
                <%: item.event_key%>
            </td>
            <td>
                  <%: item.user_token%>
            </td>               
            <td>
                <%: item.event_set_key%>
            </td>           
            <td>
                <%: item.event_type%>
            </td> 
             <td>
                <%: item.event_date%>
            </td>
            <td>
                  <%: item.event_amount%>
            </td>               
            <td>
                <%: item.event_location_key%>
            </td>           
            <td>
                <%: item.event_location_name%>
            </td> 
             <td>
                <%: item.event_location_city%>
            </td>
            <td>
                  <%: item.event_location_state%>
            </td>               
            <td>
                <%: item.event_location_country%>
            </td>           
            <td>
                <%: item.event_acknowledged%>
            </td>                   
        </tr>

    <% } %>
    </tbody>
    </table>






</asp:Content>

1 个答案:

答案 0 :(得分:0)

抛出异常究竟在哪里?

返回什么
 _readResponse.ReadToEnd();

在您尝试反序列化您的回复后,events会是什么样的?

最后,你确定这不会导致问题:

        Event eventOjb = new Event();
        events.Add(eventOjb);

不会设置任何字符串属性,这会导致空指针异常。