У меня есть веб-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 Monster 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>
}
}]
Выход:
В дополнение к моему комментарию, я приведу пример.
Я думаю, вы хотите:
namespace HobbsEventsMobile.Models
{
[DataContract]
public class Category
{
[DataMember]
public int ID;
[DataMember]
public string Name;
// everything else
}
}
Когда вы сериализуете класс, он также должен сериализовать переменные. Поскольку ваш код стоит, он просто сериализует класс, в нем ничего нет.