强类型模型/需要模型格式的输出

发布于 2024-12-06 20:47:57 字数 1197 浏览 0 评论 0原文

实际上我是这项技术的新手,我正在使用 mvc2 架构。我无法从我的模型加载数据来查看页面。我使用强类型模型EventListing.Models.EventInfo。我需要模型格式的输出。我如何使用我的选择功能

模型

public class EventInfo  
    {            
        public int OPR { get; set; }
        public int EVENT_ID { get; set; }
        public string  SUBSITE { get; set; }
public static DataTable Select()
        {
            DataTable myDataTable = new DataTable();
            Dbhelper DbHelper = new Dbhelper();
            DbCommand cmd = DbHelper.GetSqlStringCommond("SELECT * FROM WS_EVENTINFO");
            myDataTable.Load(DbHelper.ExecuteReader(cmd));
            return myDataTable;
        }

控制器

public ActionResult List()
        {            
            return View(EventModel.EventList());
        }

视图

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<EventListing.Models.EventInfo>" %>
<% foreach (var model in EventListing.Models.EventModel.EventList())
                   { %>
                <tr>
                    <td>
                        <%= Html.ActionLink(model.TITLE, "Detail", new { id = model.EVENT_ID })%>

actually i'm new to this technology, i am using mvc2 architecture. l cant able to load the data from my model to view page. i used strongly typed model EventListing.Models.EventInfo. i need output in model format. how can i use my select function

Model

public class EventInfo  
    {            
        public int OPR { get; set; }
        public int EVENT_ID { get; set; }
        public string  SUBSITE { get; set; }
public static DataTable Select()
        {
            DataTable myDataTable = new DataTable();
            Dbhelper DbHelper = new Dbhelper();
            DbCommand cmd = DbHelper.GetSqlStringCommond("SELECT * FROM WS_EVENTINFO");
            myDataTable.Load(DbHelper.ExecuteReader(cmd));
            return myDataTable;
        }

Controller

public ActionResult List()
        {            
            return View(EventModel.EventList());
        }

View

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<EventListing.Models.EventInfo>" %>
<% foreach (var model in EventListing.Models.EventModel.EventList())
                   { %>
                <tr>
                    <td>
                        <%= Html.ActionLink(model.TITLE, "Detail", new { id = model.EVENT_ID })%>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小ぇ时光︴ 2024-12-13 20:47:57

让我尝试稍微清理一下您的代码:

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>
...

对此应该做的下一个改进是将数据访问(选择静态方法)外部化到一个单独的存储库中,并具有控制器使用此存储库而不是直接调用 Select 方法来查询数据库。

Let me try to clean up your code a little:

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"))
                }
            }
        }
    }
}

then in the controller action:

public ActionResult List()
{
    var model = EventInfo.Select().ToList();
    return View(model);
}

and finally in the view:

<%@ 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>
...

The next improvement that should be done to this is to externalize the data access (the Select static method) into a separate repository and have the controller use this repository instead of directly invoking the Select method to query the database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文