我有一个返回事件的Web API:
为简洁省略了非相关部分
EventController.cs
namespace HobbsEventsMobile.Controllers
{
public class EventController : ApiController
{
// GET api/event
[HttpGet]
public List<HobbsEventsMobile.Models.Event> Get()
{
return HobbsEventsMobile.Models.Event.GetEventSummary();
}
}
这会打电话给:
Event.cs
namespace HobbsEventsMobile.Models
{
public class Event
{
public int ID;
public DateTime DateBegin;
public DateTime DateEnd;
public DateTime TimeBegin;
public int Duration;
public string Name;
public string Description;
public Category Category;
public string Location;
public Event()
{
}
public static List<Event> GetEventSummary() //List<Event> GetEventSummary()
{
List<Event> events = new List<Event>();
DataTable thisDT = new DataTable();
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
string SprocName = "HE_GetEventSummary";
SqlDataAdapter thisAdapter = new SqlDataAdapter(SprocName, connection);
thisAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
thisAdapter.Fill(thisDT);
}
foreach(DataRow row in thisDT.Rows)
{
if (Convert.ToDateTime(row["EventDateBegin"]) >= DateTime.Today)
{
Event myEvent = new Event();
myEvent.ID = Convert.ToInt32(row["EventID"]);
myEvent.DateBegin = Convert.ToDateTime(row["EventDateBegin"].ToString());
myEvent.DateEnd = Convert.ToDateTime(row["EventDateEnd"]);
myEvent.TimeBegin = Convert.ToDateTime(row["EventTimeBegin"]);
myEvent.Duration = Convert.ToInt32(row["Duration"]);
myEvent.Name = row["EventName"].ToString();
myEvent.Description = row["EventDesc"].ToString();
myEvent.Category = Category.Find(Convert.ToInt32(row["Category"]));
myEvent.Location = row["Location"].ToString();
events.Add(myEvent);
}
}
return events;
}
}
}
哪个有儿童班:
Category.cs
namespace HobbsEventsMobile.Models
{
[DataContract]
public class Category
{
public int ID;
public string Name;
public static List<Category> GetEventCategories()
{
List<Category> categories = new List<Category>();
DataTable thisDT = new DataTable();
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
string SprocName = "HE_GetEventCategories";
SqlDataAdapter thisAdapter = new SqlDataAdapter(SprocName, connection);
thisAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
thisAdapter.Fill(thisDT);
}
foreach (DataRow row in thisDT.Rows)
{
categories.Add(new Category(Convert.ToInt32(row["Category"]), row["CategoryName"].ToString()));
}
return categories;
}
public static Category Find(int id)
{
List<Category> categories = Category.GetEventCategories();
return categories.First(c => c.ID == id);
}
public Category(int id, string name)
{
this.ID = id;
this.Name = name;
}
}
}
以下是问题:在我将[DataContract]
添加到Category
之前,我收到了此错误:
<ExceptionMessage>
Type 'HobbsEventsMobile.Models.Category' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.
</ExceptionMessage>
现在错误已经消失,但它实际上并没有返回任何类别:
<Event>
<Category/>
<DateBegin>2014-05-23T00:00:00</DateBegin>
<DateEnd>2014-05-23T00:00:00</DateEnd>
<Description>
Bring your blanket or lawn chairs and enjoy a free showing of Disney's Monster's University. Movie will begin at dusk with parking on the East side of the pool. 397-9291 for more information.
</Description>
<Duration>210</Duration>
<ID>2081</ID>
<Location>Del Norte Park/Pool</Location>
<Name>Movies Under the Stars</Name>
<TimeBegin>2014-05-23T19:30:00</TimeBegin>
</Event>
提供什么?
更新1
回答评论,是的,我做了一些调试。我为此创建了一个视图:
来自ApplicationController.cs的
public ActionResult CategoryTest()
{
ViewBag.Events = Event.GetEventSummary();
return View();
}
CategoryTest.cshtml
<h2>CategoryTest</h2>
@{
foreach (HobbsEventsMobile.Models.Event thisEvent in ViewBag.Events)
{
<li>@thisEvent.Name @thisEvent.Category.Name</li>
}
}]
输出:
答案 0 :(得分:1)
继我的评论之后,我会举一个例子。
我想你想要:
namespace HobbsEventsMobile.Models
{
[DataContract]
public class Category
{
[DataMember]
public int ID;
[DataMember]
public string Name;
// everything else
}
}
序列化类时,它现在也应该序列化变量。正如你的代码所代表的那样,它只是在没有任何内容的情况下序列化类。