在MVC表格中进行分页

时间:2014-10-14 13:51:16

标签: c# .net asp.net-mvc

我想要实现的是在网格中包含分页功能(使用html表和foreach语句生成)。

我不允许使用实体框架,webgrid或任何其他第三方网格。

以下是我的任务的详细信息:

DemoTestData.cs文件

namespace Demo.Data
{
public class DemoTestData
{
    DemoTestModel objdemoTestModel = null;
    public string connectionString { get; set; }

    public SqlConnection objConnection { get; set; }
    public SqlCommand objCommand { get; set; }
    public SqlDataReader objReader { get; set; }

    public List<DemoTestModel> GetAllDemoTest()
    {
        List<DemoTestModel> listDemoTest = new List<DemoTestModel>();
        try
        {
            objConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
            objConnection.Open();
            objCommand = new SqlCommand("sp_DemoTest", objConnection);
            objCommand.CommandType = System.Data.CommandType.StoredProcedure;

            objReader = objCommand.ExecuteReader();
            if (objReader.HasRows)
            {
                while (objReader.Read())
                {
                    // list will be populated here.
                    objdemoTestModel = new DemoTestModel();
                    objdemoTestModel.Id = Convert.ToInt32(objReader["Id"].ToString());
                    objdemoTestModel.Column1 = objReader["Column1"].ToString();
                    objdemoTestModel.Column2 = objReader["Column2"].ToString();
                    objdemoTestModel.Column3 = objReader["Column3"].ToString();
                    objdemoTestModel.Column4 = objReader["Column4"].ToString();
                    objdemoTestModel.Column5 = objReader["Column5"].ToString();
                    objdemoTestModel.Column6 = objReader["Column6"].ToString();

                    listDemoTest.Add(objdemoTestModel);
                }
                //return listStateModel;
            }
        }
        catch (Exception ex)
        {
            string m = ex.Message;
        }
        finally
        {
            if (objConnection != null)
            {
                objConnection.Close();
            }
        }
        return listDemoTest;
    }
}
}

sp_DemoTest程序

CREATE PROCEDURE [dbo].[sp_DemoTest]  
AS
BEGIN  
select Id,Column1,Column2,Column3,Column4,Column5,Column6 from dbo.DemoTest  
END

DemoTestModel.cs文件

namespace Demo.Entities
{
public class DemoTestModel
{
    public int Id { get; set; }
    public string Column1 { get; set; }
    public string Column2 { get; set; }
    public string Column3 { get; set; }
    public string Column4 { get; set; }
    public string Column5 { get; set; }
    public string Column6 { get; set; }
}
}

DemoTestController.cs文件

namespace SampleProject.Controllers
{
public class DemoTestController : Controller
{
    // GET: /DemoTest/
    //Test tst = new Test();
    DemoTestData data = new DemoTestData();
    //List<CountryModel> ListModel = new List<CountryModel>();
    List<DemoTestModel> ListModel = new List<DemoTestModel>();
    //DemoTestModel demoTestModel = new DemoTestModel();

    public ActionResult Index()
    {            
        ListModel = data.GetAllDemoTest();

        return View(ListModel);
    }
}
}

Index.cshtml文件

<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Column1)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Column2)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Column3)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Column4)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Column5)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Column6)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @if (item.Column1 == "Textbox")
                {
                    @Html.TextBoxFor(modelItem => item.Column1)
                }
                else if (item.Column1 == "Dropdown")
                {
                    @Html.DropDownListFor(modelItem => item.Column1, Enumerable.Empty<SelectListItem>())
                }
                else
                {
                    @Html.DisplayFor(modelItem => item.Column1)
                }
            </td>
            <td>
                @if (item.Column2 == "Textbox")
                {
                    @Html.TextBoxFor(modelItem => item.Column2)
                }
                else if (item.Column2 == "Dropdown")
                {
                    @Html.DropDownListFor(modelItem => item.Column2, Enumerable.Empty<SelectListItem>())
                }
                else
                {
                    @Html.DisplayFor(modelItem => item.Column2)
                }
            <td>
                @if (item.Column3 == "Textbox")
                {
                    @Html.TextBoxFor(modelItem => item.Column3)

                }
                else if (item.Column3 == "Dropdown")
                {
                    @Html.DropDownListFor(modelItem => item.Column3, Enumerable.Empty<SelectListItem>())
                }
                else
                {
                    @Html.DisplayFor(modelItem => item.Column3)
                }
            </td>
            <td>
                @if (item.Column4 == "Textbox")
                {
                    @Html.TextBox("test", "", new { style = "width:130px;" })
                }
                else if (item.Column4 == "Dropdown")
                {
                    @Html.DropDownListFor(modelItem => item.Column4, Enumerable.Empty<SelectListItem>())
                }
                else
                {
                    @Html.DisplayFor(modelItem => item.Column4)
                }
            </td>
            <td>
                @if (item.Column5 == "Textbox")
                {
                    @Html.TextBox("test1", "", new { style = "width:130px;" })
                }
                else if (item.Column5 == "Dropdown")
                {
                    @Html.DropDownListFor(modelItem => item.Column5, Enumerable.Empty<SelectListItem>(), new { style = "width: 130px" })
                }
                else
                {
                    @Html.DisplayFor(modelItem => item.Column5)
                }
            </td>
            <td>
                @if (item.Column6 == "Textbox")
                {
                    @Html.TextBoxFor(modelItem => item.Column6)
                }
                else if (item.Column6 == "Dropdown")
                {
                    @Html.DropDownListFor(modelItem => item.Column6, Enumerable.Empty<SelectListItem>())
                }
                else
                {
                    @Html.DisplayFor(modelItem => item.Column6)
                }
            </td>
        </tr>
    }
</table>

(if..else条件用于生成动态控件)

这是渲染视图

this is the rendered view

如果有人能提供解决方案,那将是非常好的。

0 个答案:

没有答案