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

标签 javascript c# jquery asp.net asp.net-mvc

这就是我从模型中实现下拉菜单的方式。我只是想检查这是否是正确的方法或者是否有更简单的方法?看起来很复杂。

我的列表的型号:

public class ApplicationRolesDropdownListDetailViewModel
{
    public String RoleId { get; set; }
    public String ApplicationRoleName { get; set; }
}

进入我的 View 的 View 模型

public class ApplicationRolesDropdownListViewModel
{
    public SelectList Roles { get; set; }
}

获取项目列表并将其放入 View 下拉列表中的 Controller :

    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;
    }

最佳答案

尝试这样

ViewModel:

public class ApplicationRolesViewModel
{
    // Display Attribute will appear in the Html.LabelFor
    [Display(Name = "User Role")]
    public string RoleId { get; set; }
    public IEnumerable<SelectListItem> Roles { get; set; }
}

Controller :

public ActionResult NewRole()
    {
        var roleData = new IEnumerable<SelectListItem>();
        applicationRolesData.GetAllApplicationRoles().Foreach(x =>
                   roleData.Add( new SelectListItem
                        {
                            Value = x.RoleId.ToString(),
                            Text = x.ApplicationRoleName
                        });
       );
        ApplicationRolesViewModel ardlvm = new ApplicationRolesViewModel();
        ardlvm.Roles = new SelectList(roleData , "Value", "Text")
        return View("~/Views/Users/Modals/AddRole.cshtml", ardlvm);
    }

查看:

@model ApplicationRolesViewModel

@Html.LabelFor(m => m.RoleId)
@Html.DropDownListFor(m => m.RoleId, Model.Roles)

并在Jquery中获取下拉菜单当前选定的值:

$('#RoleId').val();

获取当前选定的文本:

$('#RoleId:selected').text();

关于javascript - 在 ASP.NET MVC 中填充下拉列表的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52245682/

相关文章:

javascript - PhoneGap 上的 LocalStorage 限制

javascript - 无法让 Google Plus 与其他社交媒体按钮保持一致

c# - 在 winforms 中取消工作线程

c# - 单点触控/三20 : Add many items to the first page of TTLauncherView

c# - ASP.NET Core 项目 - 发布不显示 "Database Migrations"选项

javascript - 将 jQuery 操作应用于特定控件

javascript - 使用 jquery 替换用字符串替换数字

javascript - 将 Java 代码重写为 JS - 从字节创建音频?

php - 使用 jquery 和 php 将数据与上传图像一起保存到 mysql

Jquery UI Widget 问题 - 圆 Angular