[英]populate list<object> with push json data and display table
Im using aspx engine. 我正在使用aspx引擎。 I've been trying to push data into my list but i don't know how. 我一直在尝试将数据推送到列表中,但我不知道如何。 every time i run the program it says create new instance of an object..im not too sure what they talking about..but i did create an instance of the class but still didnt work Can anyone please help me 每次我运行程序时,它都会说创建一个对象的新实例..im我不太确定他们在说什么..但是我确实创建了该类的实例,但仍然无法正常工作有人可以帮我吗
this is my trying to populate data in my homecontroller 这是我试图在我的家庭控制器中填充数据
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();
}
this is my Event class that is in the model folder 这是我在模型文件夹中的事件类
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; }
}
} }
this is my html trying to step through the list and create a table and error message comes on the foreach loop that says Object reference not set to an instance of an object 这是我的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>
Where exactly is the exception being thrown? 到底在哪里抛出异常?
What is being returned from 从什么返回
_readResponse.ReadToEnd();
After you try to deserialize your response, what does events
look like? 尝试反序列化响应后, events
什么样的?
Finally, are you sure this isn't causing the problem: 最后,您确定这不会引起问题:
Event eventOjb = new Event();
events.Add(eventOjb);
None of the string properties will be set, which would lead to a null pointer exception. 没有设置任何字符串属性,这将导致空指针异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.