0

我对下面的代码有一个愿景,我的想法是返回一个 HttpPost UserRoles 列表。我的 UserRole 模型如下:

idUser
idRole

我能怎么做?

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>UserRole</legend>

       @Html.Label("Select User:") @Html.DropDownList("UserList", String.Empty)
        @{            
            foreach (var item in ViewBag.Roles)
            {
               <div id="@item.NameLower">
                   <b>@item.Name</b>
                   <input type="checkbox" value="@item.NameLower" />
               </div>

            }
        }
        <p>
            <input type="submit" value="Create"/>
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to list", "Index")
</div>

我的控制器:

public ActionResult Create()
    {
        ViewBag.Roles = db.roles.ToList();
        ViewData["UserList"] = new SelectList(db.users, "Id", "Nome");

        return View();
    }

还有我的数据库:
http://img12.imageshack.us/img12/3227/userrole.jpg

我创建 de ViewModel,例如:

namespace Sacer.ViewModels
{
    public class UserRoleModel
    {
        public List<User> usuario
        {
            get
            {
                return db.users.ToList();
            }
        }

        private SacerContext db = new SacerContext();
        public List<Role> role
        {
            get
            {
                return db.roles.ToList();
            }
        }
    }
}`

和我的控制器`

public ActionResult Create()
        {
            ViewData["ListaUsuarios"] = new SelectList(db.users, "Id", "Nome");        
            var model = new UserRoleModel();
            return View(model);
        }

我的观点:

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>UserRole</legend>

        @Html.Label("Selecione o Usuário:") @Html.DropDownList("ListaUsuarios", String.Empty)
        @{
                foreach (Sacer.Models.Role role in Model.role)
                {
                    <div>
                        <input type="checkbox" value="@role.NomeMinusculo" name="Roles" /> @role.Nome
                    </div>
                }                  
         }
        <p>
            <input type="submit" value="Criar" onclick="GerarLista" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Voltar à lista", "Index")
</div>

但是,我如何在表 userRoles 中插入?

4

0 回答 0