在 ASP.NET MVC 中填充下拉列表的正确方法

这就是我从我的模型中实现下拉列表的方式。我只是想检查这是否是正确的方法还是有更简单的方法?看起来相当复杂。


我的清单型号:


public class ApplicationRolesDropdownListDetailViewModel

{

    public String RoleId { get; set; }

    public String ApplicationRoleName { get; set; }

}

进入我的视图的视图模型


public class ApplicationRolesDropdownListViewModel

{

    public SelectList Roles { get; set; }

}

获取项目列表并将其放在视图中的下拉列表中的控制器:


    public ActionResult NewRole()

    {

        var applicationRoles = applicationRolesData.GetAllApplicationRoles();

        ApplicationRolesDropdownListViewModel ardlvm = new ApplicationRolesDropdownListViewModel();

        ardlvm.Roles = new SelectList(applicationRoles, "RoleId", "ApplicationRoleName");

        return View("~/Views/Users/Modals/AddRole.cshtml", ardlvm);

    }

我的观点:


    <div class="form-group">

        @Html.DropDownListFor(m => m.Roles,Model.Roles, new { @id = "role", @class = "dropdown" })

    </div>

此外,当我通过 javascript 从列表中选择一个项目时,我似乎无法获得 RoleId。


编辑:添加了我的 GetApplicationRoles


    public List<ApplicationRolesDropdownListViewModel > GetAllApplicationRoles()

    {

        List<ApplicationRolesDropdownListViewModel > data = new List<ApplicationRolesDropdownListViewModel >();

        try

        {

            var applicationRoles = dbContext.AspNetRolesExtendedDetails.ToList();

            data = (from ar in applicationRoles

                    join a in dbContext.AspNetApplications

                     on ar.ApplicationId equals a.Id

                    select new ApplicationRolesDropdownListViewModel 

                    {

                        RoleId = ar.Id,

                        ApplicationRoleName = ar.Name + " ( " + a.Name + " )"

                    }).ToList();

        }

        catch (Exception e)

        {

            logger.Error(e, AspNetEventLogs.NotFound);

        }

        return data;

    }


千万里不及你
浏览 308回答 1
1回答

慕运维8079593

像这样试试视图模型:public class ApplicationRolesViewModel{&nbsp; &nbsp; // Display Attribute will appear in the Html.LabelFor&nbsp; &nbsp; [Display(Name = "User Role")]&nbsp; &nbsp; public string RoleId { get; set; }&nbsp; &nbsp; public IEnumerable<SelectListItem> Roles { get; set; }}控制器:public ActionResult NewRole()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var roleData = new IEnumerable<SelectListItem>();&nbsp; &nbsp; &nbsp; &nbsp; applicationRolesData.GetAllApplicationRoles().Foreach(x =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;roleData.Add( new SelectListItem&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Value = x.RoleId.ToString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text = x.ApplicationRoleName&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp;);&nbsp; &nbsp; &nbsp; &nbsp; ApplicationRolesViewModel ardlvm = new ApplicationRolesViewModel();&nbsp; &nbsp; &nbsp; &nbsp; ardlvm.Roles = new SelectList(roleData , "Value", "Text")&nbsp; &nbsp; &nbsp; &nbsp; return View("~/Views/Users/Modals/AddRole.cshtml", ardlvm);&nbsp; &nbsp; }查看:@model ApplicationRolesViewModel@Html.LabelFor(m => m.RoleId)@Html.DropDownListFor(m => m.RoleId, Model.Roles)并获取当前选择的下拉列表值Jquery:$('#RoleId').val();要获取当前选定的文本:$('#RoleId:selected').text();
打开App,查看更多内容
随时随地看视频慕课网APP