参数值为null

时间:2014-03-29 05:31:02

标签: c# asp.net-mvc null html-helper

以下代码显示每个国家/地区的复选框列表。打算保存选中的复选框。 但是当单击提交按钮时,在方法
中 newmodel中的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 个答案:

答案 0 :(得分: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" />
}

希望它有所帮助! :)