如何用复选框显示数据表?

时间:2014-02-03 16:56:17

标签: c# asp.net-mvc checkbox

我有一个非常简单的数据表返回:

报告1和一个复选框 Report2和一个复选框

这是我的观点:

@model GRM_Reports.Models.ReportsModel

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}




@using (Html.BeginForm("Index","Report", FormMethod.Post))
{
        <table>
        <tr style="font-weight:bold">

            <td>
                Report Name
            </td>
            <td>
                Number Of Rows
            </td>
            <td>
                Number Of Errors
            </td>
            <td>
                Run Report
            </td>
        </tr>

            @foreach (var report in Model.ReportList)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modeltem => report.ReportName)
                    </td>
                    <td>
                        @Html.DisplayFor(modeltem => report.NumberOfRows)
                    </td>
                    <td>
                       @Html.DisplayFor(modeltem => report.NumberOfErrors)
                    </td>
                    <td>
                       @Html.CheckBoxFor(modeltem => report.checkBox)
                    </td>
              </tr>
            }
                <tr>
                <td colspan="4">
                    <button type="submit" >Submit Request</button>
                </td>
            </tr>
        </table>
}

当我单击提交按钮时,ReportList为空。

namespace GRM_Reports.Controllers
{
    public class ReportController : Controller
    {
    public ActionResult Index()
    {
        var model = new ReportsModel();
        Database db = new Database();
        DataTable dt = new DataTable();
        dt = db.GetReportStatus("1/31/2014");
        model.ReportList = new List<Models.Common.Report>();
                    foreach (DataRow row in dt.Rows)
                    {
                        model.ReportList.Add(
                    new Models.Common.Report() 
                    { ReportName = row["Report"].ToString(), 
                      NumberOfRows = int.Parse(row["NumberOfRows"].ToString()), 
                      NumberOfErrors = int.Parse(row["NumberOfErrors"].ToString()),
                      HasData = row["hasData"].ToString()
                    });

        }
        return View(model);
    }




    [HttpPost]
    public ActionResult Index(ReportsModel model)
    {
           foreach (var report in model.ReportList)
           {


           }
        return View();
    }
}
}

我认为此方法存在问题:          [HttpPost]          公共ActionResult索引(ReportsModel模型)

传递给此方法的模型为null。有什么想法吗?

谢谢。

2 个答案:

答案 0 :(得分:2)

以下是您的解决方案,我通过添加一个ViewModel -

来稍微修改了您的模型
public class ReportsModel
{
    public List<ReportModel> ReportsList { get; set; }
}

public class ReportModel
{
    public Report Report { get; set; }
    public bool IsChecked { get; set; }
}

public class Report
{
    public string ReportName { get; set; }
    public DateTime ReportDate { get; set; }
    public int NumberOfRows { get; set; }
    public int NumberOfErrors { get; set; }
    public string HasData { get; set; }
}

然后在索引操作中,我们构建模型并将其发送到视图 -

    public ActionResult Index()
    {
        ReportsModel model = new ReportsModel();
        model.ReportsList = new List<ReportModel>();
        model.ReportsList.Add(new ReportModel() { Report = new Report() { ReportName = "Report1" }, IsChecked = false });
        model.ReportsList.Add(new ReportModel() { Report = new Report() { ReportName = "Report2" }, IsChecked = false });
        model.ReportsList.Add(new ReportModel() { Report = new Report() { ReportName = "Report3" }, IsChecked = false });
        return View(model);
    }

索引视图将显示复选框 -

@model MVC.Controllers.ReportsModel

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm("Submit", "Checkbox", FormMethod.Post))
{
    for (int i = 0; i < Model.ReportsList.Count; i++)
    {
        @Html.CheckBoxFor(m => m.ReportsList[i].IsChecked) @Model.ReportsList[i].Report.ReportName
        <br />
    }
    <input type="submit" value="Click" />
}

输出 -

enter image description here

选择一些复选框并单击“提交”按钮后,它将提交给“提交”操作,您可以获取模型中的所有选定值,如下所示 -

    public ActionResult Submit(ReportsModel model)
    {
        // Do something
        return null;
    }

enter image description here

<强>更新

要获取Model的所有属性,您需要使用HiddenFields,如下所示 -

for (int i = 0; i < Model.ReportsList.Count; i++)
{
    @Html.CheckBoxFor(m => m.ReportsList[i].IsChecked) @Model.ReportsList[i].Report.ReportName
    <br />

    @Html.HiddenFor(m => m.ReportsList[i].Report.ReportName);
    @Html.HiddenFor(m => m.ReportsList[i].Report.ReportDate);
    @Html.HiddenFor(m => m.ReportsList[i].Report.NumberOfRows);
    @Html.HiddenFor(m => m.ReportsList[i].Report.NumberOfErrors);
    @Html.HiddenFor(m => m.ReportsList[i].Report.HasData);
}

答案 1 :(得分:0)

//模型

namespace Reports.Models.Common 
{
    public class Report
    {
        public string ReportName { get; set; }
        public DateTime ReportDate { get; set; }
        public int NumberOfRows { get; set; }
        public int NumberOfErrors { get; set; }
        public string HasData { get; set; }
        public bool Remember {get;set;}
    }
}

//视图

@Html.CheckboxFor(x => x.Remember)