窗体上的ViewModel null返回ASP.NET MVC4 C#

时间:2017-04-04 05:16:31

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

我有一个看起来像这样的viewmodel:

public class SecurityProfileViewModel
{
    public string SecurityProfileNum { get; set; }
    public List<String> SecurityProfilePDRGroups { get; set; }
    public List<WrReportConfiguration> SecurityProfilePDR { get; set; }
    public List<String> SecurityProfileUDRGroups { get; set; }
    public List<WrReportConfiguration> SecurityProfileUDR { get; set; }
}

控制器GET方法如下所示:

    // GET: /Admin/

    public ActionResult ConfigureUserPermissions(string securityProfile)
    {
        ReportsConfigurationBuilder rcb = new ReportsConfigurationBuilder();
        rcb.BuildSecurityProfileReportConfiguration(Convert.ToInt32(securityProfile));

        SecurityProfileViewModel spvm = new SecurityProfileViewModel
        {
            SecurityProfileNum = securityProfile,
            SecurityProfilePDRGroups = rcb.SecurityProfilePDRGroupsList,
            SecurityProfilePDR = rcb.SecurityProfilePDRConfig,
            SecurityProfileUDRGroups = rcb.SecurityProfileUDRGroupsList,
            SecurityProfileUDR = rcb.SecurityProfileUDRConfig,
        };

        return View(spvm);
    }

该视图会生成一个包含相关复选框的报告列表。代码如下所示:

@model ClearWeighWebReporting.ViewModels.SecurityProfileViewModel

@{
    ViewBag.Title = "ConfigureUserPermissions";
}

<p>Security Profile @Html.DisplayFor(modelItem => Model.SecurityProfileNum)</p>

@using (Html.BeginForm())
{
    <p>Pre-defined Reports</p>

    foreach (String pdrGroup in Model.SecurityProfilePDRGroups)
    {
        @Html.DisplayFor(modelItem => pdrGroup)

        <table>
            @foreach (ClearWeighWebReporting.Models.WrReportConfiguration pdrc in Model.SecurityProfilePDR)
            {
                if (pdrc.ReportGroup == pdrGroup)
                {
                    <tr>
                        <td>
                            @Html.HiddenFor(modelItem => pdrc.ReportID)
                            @Html.DisplayFor(modelItem => pdrc.ReportTitle)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => pdrc.ViewPermissionString)
                            @Html.CheckBoxFor(modelItem => pdrc.ViewPermission)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => pdrc.EditPermissionString)
                            @Html.CheckBoxFor(modelItem => pdrc.EditPermission)
                        </td>
                    </tr>
                }
            }
        </table>
    }

    ... other view code

    <input id="Submit1" type="submit" value="Submit" />    
}

视图呈现正常:

image of page

但是当我检查/取消选中值并提交表单时,返回POST方法的视图模型为NULL:

debugger showing NULL view model

如何在视图模型中返回复选框值?这是一个具有约束力的问题吗?

0 个答案:

没有答案