我如何从我的视图返回控制器的List <userroles>?</userroles>

时间:2012-08-16 18:42:51

标签: c# asp.net-mvc razor

我对下面的代码有一个愿景,我的想法是返回一个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中插入?

0 个答案:

没有答案