[asp.net-mvc]参数值为 null

标签: Asp.net-MVC C#
发布时间: 2016/10/9 6:06:36
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

下面的代码显示列表的复选框的国家为每个。他打算为保存哪些复选框被选中。 但是,当单击提交按钮时,在方法中
ActionResult Index(UserModel newmodel) 在盈利性,参数 SelectedSecurityGroup、 SelectedSecurityObject 和 SecurityGroups 的值为 null。

任何想法这段代码有什么错误?

在 UserModel.cs

public class UserModel
{
    public string SelectedSecurityGroup { get; set; }
    public string SelectedSecurityObject { get; set; }

    [DisplayName("Security Group")]
    public virtual ICollection<SecurityGroup> SecurityGroups { get; set; }
}

public class SecurityGroup
{
    public int Id { get; set; }
    public string SecurityGroupName { get; set; }
    public bool Active { get; set; }
}

在 UserController.cs

[HttpGet]
public ActionResult Index()
{
    UserModel objUserModel = new UserModel();

    List<SecurityGroup> lstSecurityGroup = FillViewBag();
    objUserModel.SecurityGroups = lstSecurityGroup;
    return View(objUserModel);
}

[HttpPost]
public ActionResult Index(UserModel newmodel)
{
    string strtest = "";

    //Code to save data

    return View(newmodel);
}

private List<SecurityGroup> FillViewBag(UserModel model = null)
{
    List<SecurityGroup> lstSecurityGroup = new List<SecurityGroup>();
    lstSecurityGroup.Add(new SecurityGroup { Id = 1, SecurityGroupName = "India", Active = true });
    lstSecurityGroup.Add(new SecurityGroup { Id = 2, SecurityGroupName = "USA", Active = true });
    lstSecurityGroup.Add(new SecurityGroup { Id = 3, SecurityGroupName = "Pakistan", Active = false });
    lstSecurityGroup.Add(new SecurityGroup { Id = 4, SecurityGroupName = "Nepal", Active = false });

    return lstSecurityGroup;
}

在 Index.cshtml

@model Example.User.Web.Models.UserModel

<!DOCTYPE html>
<html>
<head>
    <title>Dashboard</title>
    <script src="~/Scripts/jquery-1.11.0.js"></script>
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

</head>
<body>
    <div>
        <div>
            <div id="lists">
                @Html.Partial("SecurityListsView", Model)
            </div>
        </div>
    </div>
</body>
</html>

在 SecurityListsView.cshtml

@model Example.User.Web.Models.UserModel

@using (Ajax.BeginForm("Index", "User", new AjaxOptions() { UpdateTargetId = "lists" }))
{    
    <table>
        @{ int i = 0; }
        @foreach (var item in Model.SecurityGroups )
        {
            <tr>
                <td>
                    @Html.CheckBox("fileName", item.Active)
                    @Html.Hidden("fileId", item.Id)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.SecurityGroupName)
            </tr>
            i++;
        }
    </table>
    <input type="submit" name="btn1" value="Save" />
}

解决方法 1:

最后有它的工作。下面是更正的代码︰ 在 SecurityListsView.cshtml

@model Example.User.Web.Models.UserModel


@using (Html.BeginForm("Index", "User", "POST"))
{
    <table>
        @{ int i = 0; }
        @foreach (var newitem in Model.SecurityGroups)
        {
            <tr>
                <td>
                    @Html.CheckBoxFor(model => model.SecurityGroups[i].Active)
                    @Html.HiddenFor(model => model.SecurityGroups[i].Id, "Value")
                </td>
                <td>
                @Html.DisplayFor(model => model.SecurityGroups[i].SecurityGroupName)

            </tr>
            i++;
        }
    </table>
    <input type="submit" name="btn1" value="Save" />
}

希望它能帮助一个人 !:)

赞助商