Asp.net mvc 2 强类型模型/需要模型格式的输出
事实上,我是这项技术的新手,我使用的是mvc2架构。我无法将数据从我的模型加载到查看页面。我使用了强类型模型EventListing.Models.EventInfo。我需要模型格式的输出。如何使用选择功能 模型 控制器Asp.net mvc 2 强类型模型/需要模型格式的输出,asp.net-mvc-2,Asp.net Mvc 2,事实上,我是这项技术的新手,我使用的是mvc2架构。我无法将数据从我的模型加载到查看页面。我使用了强类型模型EventListing.Models.EventInfo。我需要模型格式的输出。如何使用选择功能 模型 控制器 public ActionResult List() { return View(EventModel.EventList()); } 看法 让我试着清理一下您的代码: public class
public ActionResult List()
{
return View(EventModel.EventList());
}
看法
让我试着清理一下您的代码:
public class EventInfo
{
public int OPR { get; set; }
public int EVENT_ID { get; set; }
public string SUBSITE { get; set; }
... some other properties that you might want to use
public static IEnumerable<EventInfo> Select()
{
var helper = new Dbhelper();
using (var cmd = helper.GetSqlStringCommond("SELECT * FROM WS_EVENTINFO"))
using (var reader = helper.ExecuteReader(cmd))
{
while (reader.Read())
{
yield return new EventInfo
{
OPR = reader.GetInt32(reader.GetOrdinal("OPR")),
EVENT_ID = reader.GetInt32(reader.GetOrdinal("EVENT_ID")),
SUBSITE = reader.GetString(reader.GetOrdinal("SUBSITE"))
}
}
}
}
}
最后,我认为:
<%@ Page
Language="C#"
Inherits="System.Web.Mvc.ViewPage<IEnumerable<EventInfo>>" %>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.ActionLink(item.TITLE, "Detail", new { id = item.EVENT_ID }) %>
</td>
...
...
对此应该做的下一个改进是将数据访问(选择静态方法)外部化到一个单独的存储库中,并让控制器使用此存储库,而不是直接调用选择方法来查询数据库。您的控制器实际上并不调用您提供的模型。我建议您实际选择数据时,您需要获得要启动的
EventInfo
对象的列表,并显示EventModel.EventList()
code。这个问题是无法回答的。
public class EventInfo
{
public int OPR { get; set; }
public int EVENT_ID { get; set; }
public string SUBSITE { get; set; }
... some other properties that you might want to use
public static IEnumerable<EventInfo> Select()
{
var helper = new Dbhelper();
using (var cmd = helper.GetSqlStringCommond("SELECT * FROM WS_EVENTINFO"))
using (var reader = helper.ExecuteReader(cmd))
{
while (reader.Read())
{
yield return new EventInfo
{
OPR = reader.GetInt32(reader.GetOrdinal("OPR")),
EVENT_ID = reader.GetInt32(reader.GetOrdinal("EVENT_ID")),
SUBSITE = reader.GetString(reader.GetOrdinal("SUBSITE"))
}
}
}
}
}
public ActionResult List()
{
var model = EventInfo.Select().ToList();
return View(model);
}
<%@ Page
Language="C#"
Inherits="System.Web.Mvc.ViewPage<IEnumerable<EventInfo>>" %>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.ActionLink(item.TITLE, "Detail", new { id = item.EVENT_ID }) %>
</td>
...