代码之家  ›  专栏  ›  技术社区  ›  JonathanTien

在MVC中显示标准数据表

  •  39
  • JonathanTien  · 技术社区  · 16 年前

    理想情况下,我只希望能够有一个对象,我可以在视图中迭代。

    我真的有点不知所措 the article from the "Gu" 我只能总结一下,我必须传回一个ViewData对象来代替??我是不是疯了?

    乔恩

    3 回复  |  直到 9 年前
        1
  •  72
  •   Elijah66    16 年前

    这一点都不“错”,只是酷家伙通常不会用MVC。顺便说一句,我希望ASP.NETMVC的一些早期演示不要试图同时填充LINQtoSQL。它非常棒,非常适合MVC,当然,但它不是必需的。 MVC并没有阻止您使用ADO.NET

    控制器操作:

    public ActionResult Index()
    {
        ViewData["Message"] = "Welcome to ASP.NET MVC!";
    
        DataTable dt = new DataTable("MyTable");
        dt.Columns.Add(new DataColumn("Col1", typeof(string)));
        dt.Columns.Add(new DataColumn("Col2", typeof(string)));
        dt.Columns.Add(new DataColumn("Col3", typeof(string)));
    
        for (int i = 0; i < 3; i++)
        {
            DataRow row = dt.NewRow();
            row["Col1"] = "col 1, row " + i;
            row["Col2"] = "col 2, row " + i;
            row["Col3"] = "col 3, row " + i;
            dt.Rows.Add(row);
        }
    
        return View(dt); //passing the DataTable as my Model
    }
    

    <table border="1">
        <thead>
            <tr>
                <%foreach (System.Data.DataColumn col in Model.Columns) { %>
                    <th><%=col.Caption %></th>
                <%} %>
            </tr>
        </thead>
        <tbody>
        <% foreach(System.Data.DataRow row in Model.Rows) { %>
            <tr>
                <% foreach (var cell in row.ItemArray) {%>
                    <td><%=cell.ToString() %></td>
                <%} %>
            </tr>
        <%} %>         
        </tbody>
    </table>
    

    现在,我在这里违反了很多ASP.NETMVC的原则和“最佳实践”,所以请理解这只是一个简单的演示。创建DataTable的代码应该驻留在控制器之外的某个地方,视图中的代码最好与partial或html助手隔离,这是您可以使用的几种方法 应该 做事。

    ,如果视图应该呈现它们(关注点的分离决定了观点不应该为 创建 在本例中,我将DataTable作为实际的视图模型传递,但您也可以将其放入viewdatacollection中。或者,您可以创建一个特定的IndexViewModel类,该类包含DataTable和其他对象,例如欢迎消息。

    我希望这有帮助!

        2
  •  42
  •   Chiramisu    10 年前

    下面是用Razor语法给出的答案

     <table border="1" cellpadding="5">
        <thead>
           <tr>
              @foreach (System.Data.DataColumn col in Model.Columns)
              {
                 <th>@col.Caption</th>
              }
           </tr>
        </thead>
        <tbody>
        @foreach(System.Data.DataRow row in Model.Rows)
        {
           <tr>
              @foreach (var cell in row.ItemArray)
              {
                 <td>@cell.ToString()</td>
              }
           </tr>
        }      
        </tbody>
    </table>
    
        3
  •  6
  •   Jonas Lincoln    14 年前

    将数据集放入列表中。。。。。 我有一个存储库模式,下面是一个从老式asmxweb服务获取数据集的示例 专用只读CISOnlineSRVDEV.ServiceSoapClient\u ServiceSoapClient;

        public Get_Client_Repository()
            : this(new CISOnlineSRVDEV.ServiceSoapClient())
        {
    
        }
        public Get_Client_Repository(CISOnlineSRVDEV.ServiceSoapClient serviceSoapClient)
        {
            _ServiceSoapClient = serviceSoapClient;
        }
    
    
        public IEnumerable<IClient> GetClient(IClient client)
        {
            // ****  Calling teh web service with passing in the clientId and returning a dataset
            DataSet dataSet = _ServiceSoapClient.get_clients(client.RbhaId,
                                                            client.ClientId,
                                                            client.AhcccsId,
                                                            client.LastName,
                                                            client.FirstName,
                                                            "");//client.BirthDate.ToString());  //TODO: NEED TO FIX
    
            // USE LINQ to go through the dataset to make it easily available for the Model to display on the View page
            List<IClient> clients = (from c in dataSet.Tables[0].AsEnumerable()
                                     select new Client()
                                     {
                                         RbhaId = c[5].ToString(),
                                         ClientId = c[2].ToString(),
                                         AhcccsId = c[6].ToString(),
                                         LastName = c[0].ToString(), // Add another field called   Sex M/F  c[4]
                                         FirstName = c[1].ToString(),
                                         BirthDate = c[3].ToDateTime()  //extension helper  ToDateTime()
                                     }).ToList<IClient>();
    
            return clients;
    
        }
    

    IClient client = (IClient)TempData["Client"];
    
    // Instantiate and instance of the repository 
    var repository = new Get_Client_Repository();
    // Set a model object to return the dynamic list from repository method call passing in the parameter data
    var model = repository.GetClient(client);
    
    // Call the View up passing in the data from the list
    return View(model);
    

    在视图中很容易:

    @model IEnumerable<CISOnlineMVC.DAL.IClient>
    
    @{
        ViewBag.Title = "CLIENT ALL INFORMATION";
    }
    
    <h2>CLIENT ALL INFORMATION</h2>
    
    <table>
        <tr>
            <th></th>
            <th>Last Name</th>
            <th>First Name</th>
            <th>Client ID</th>
            <th>DOB</th>
            <th>Gender</th>
            <th>RBHA ID</th>
            <th>AHCCCS ID</th>
        </tr>
    
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.ActionLink("Select", "ClientDetails", "Cis", new { id = item.ClientId }, null) |
            </td>
            <td>
                @item.LastName
            </td>
            <td>
                @item.FirstName
            </td>
             <td>
                @item.ClientId
            </td>
             <td>
                @item.BirthDate
            </td>
             <td>
                Gender @* ADD in*@
            </td>
             <td>
                @item.RbhaId
            </td>
             <td>
                @item.AhcccsId
            </td>
        </tr>
    }
    
    </table>