对象未在MVC Razor表单中返回

时间:2016-07-07 15:59:46

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

我有一个简单的用户管理器控制台来更改用户的角色。两种主要方法是:

public ActionResult Index()
{
    //Use Entity Framework and get all users and their roles
}

[HttpPost]
public async Task<ActionResult> Index(UserRoleManagerContainer mgr)
{
    //recieve updated object and reassign roles
}

在我的C#页面中,我有以下代码,它会在页面加载时显示所有用户及其角色:

@using (Html.BeginForm())
{
    <fieldset>
        @for (int i = 0; i < Model.UsersInRoles.Count; i++)
        {
            <div style="margin-bottom:20px">
                <div class="editor-label">
                    @Model.UsersInRoles[i].User.UserName
                </div>
                <div class="editor-label">
                    @for (int j = 0; j < Model.UsersInRoles[i].Roles.Count; j++)
                    {
                        <div class="editor-label">
                            @Html.TextBoxFor(m => Model.UsersInRoles[i].Roles[j].Name)
                        </div>
                    }
                </div>
            </div>
        }
    </fieldset>

    <input type="submit" value="update roles" />
}

不幸的是,当HttpPost触发时我得到了一个空对象。

这是我想要传回的对象:

public class UserRoleManagerContainer
{
    public List<string> Roles { get; set; }
    public List<UserInRoles> UsersInRoles { get; set; }

    public UserRoleManagerContainer()
    {
        Roles = new List<string>();
        UsersInRoles = new List<UserInRoles>();
    }
}
public class UserInRoles
{
    public ApplicationUser User { get; set; }
    public List<IdentityRole> Roles { get; set; }
}

0 个答案:

没有答案