質問

The below code displays list of countries with checkbox for each. The intend is to save which checkbox was checked. But when the submit button is clicked, in the method
ActionResult Index(UserModel newmodel) in the newmodel, the value of parameters SelectedSecurityGroup, SelectedSecurityObject and SecurityGroups is null.

Any idea what is wrong with this code?

In 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; }
}

In 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;
}

In 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>

In 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" />
}
役に立ちましたか?

解決

Finally got it working. Below is the corrected code: In 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" />
}

Hope it helps someone! :)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top