Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何返回列表<;用户角色>;从我的角度看控制器的性能?_C#_Asp.net Mvc_Razor - Fatal编程技术网

C# 如何返回列表<;用户角色>;从我的角度看控制器的性能?

C# 如何返回列表<;用户角色>;从我的角度看控制器的性能?,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我对下面的代码有一个设想,我的想法是返回一个HttpPost用户角色列表。我的用户角色模型如下: idUser idRole 我该怎么办 @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>UserRole</legend> @Html.Label("Select User:") @Html.DropDownL

我对下面的代码有一个设想,我的想法是返回一个HttpPost用户角色列表。我的用户角色模型如下:

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>
和我的数据库:

我创建了视图模型,如:

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();
            }
        }
    }
}`
我的看法是:

@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>
@使用(Html.BeginForm())
{
@Html.ValidationSummary(true)
用户角色
@Label(“selecioneousuário:”)@Html.DropDownList(“ListaUsuarios”,String.Empty)
@{
foreach(Sacer.Models.Role-Model.Role中的角色)
{
@角色,诺姆
}                  
}

} @ActionLink(“Voltarálista”,“Index”)

<>但是,我如何插入表USER角色?

不可回答的,不知道视图包中的内容。并且考虑用一个PraveVIEW模型替换VIEWBACK。在ViewBag中,我的数据库中存在所有类型的角色。角色包含名称和ID。如果UserRole表中存在用户ID和角色ID,则他有权访问该视图。如果没有,则显示错误页面。为什么角色列表标有“Select User:”?至少现在您的问题更清楚了:如何显示和检索项目的选中列表。
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>