简体   繁体   English

如何在用户的下拉列表中显示名称而不是 ID 从另一个表到 select MVC

[英]How to show name instead of ID from another table in dropdown list for user to select MVC

I am currently working on a project, I have 2 tables for now, User and Designation.我目前正在做一个项目,我现在有 2 个表,User 和 Designation。 When a user registers they will have to select a designation, which means that DesignationID is a FK in User table, however in my view, I do not want to display the ID I want it to display the name in a dropdown list that the user can select when registering.当用户注册时,他们必须指定 select,这意味着 DesignationID 是 User 表中的 FK,但是在我看来,我不想显示 ID 我希望它在用户的下拉列表中显示名称注册时可以select。 I have tried a few solutions however I am still getting an error which I'm not sure how to fix as I am new to asp.net, Please can you assist?我已经尝试了一些解决方案,但我仍然遇到错误,我不确定如何解决,因为我是 asp.net 的新手,请你帮忙? Here is what I've tried这是我试过的

User model:用户model:

public partial class User
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int UserID { get; set; }
    public User user { get; set; }

    [Required]
    [Display(Name ="First Name")]
    public string FirstName { get; set; }

    [Required]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }

    [Required]
    [Display(Name = "Designation")]
    public int DesignationID { get; set; }
    public virtual Designation designation { get; set; }

    [Required]
    [Display(Name = "Email")]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    [Required]
    [Display(Name = "Username")]
    public string UserName { get; set; }

    [Required]
    [Display(Name = "Password")]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [Required]
    [Display(Name = "Address")]
    public string Address { get; set; }

    [Required]
    [Display(Name = "Date of birth")]
    [DataType(DataType.Date)]
    public System.DateTime DOB { get; set; }
}

Designation model:指定 model:

public partial class Designation
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]

    public int DesignationID { get; set; }

    [Required]
    [Display(Name ="Designation Name")]
    public string DesignationName { get; set; }

    public virtual ICollection<User> users { get; set; }
}

Controller code I used我用的Controller码

public class HomeController : Controller
{
    private CompanyPrintersEntities db = new CompanyPrintersEntities();
    public ActionResult Index()
    {  
        return View(db.Users.Include(us => us.user).Include(us => us.designation).ToList());
    }
}

View:看法:

<div class="form-group">
           
 @Html.LabelFor(model => model.DesignationID, htmlAttributes: new { @class = "control-label col-md-2" })

    <div class="col-md-10">
        @foreach (User item in Model)
        {
            <p>User @item.User.Name is in Designation @item.Designation.Name</p>
        }
    </div>
</div>

Error I am getting:我得到的错误:

错误

I am using Entity Framework DB first approach.我正在使用 Entity Framework DB 优先方法。

I think you must use thenInclude我认为你必须使用 thenInclude

 db.Users.Include(us => us.user).ThenInclude(us => us.designation).ToList()

create a list in your model as在您的 model 中创建一个列表作为

public List<Designation> DesignationsList { get; set; }

you can use this list in your controller as您可以在您的 controller 中使用此列表作为

Designations.DesignationsList = _db.Designations.ToList();

then use the list in drop down as然后使用下拉列表作为

 <div class="row form-group"> <div class="col-md-2"> <label asp-for="DesignationID" class="control-label"></label> </div> <div class="col-md-10"> <select id="drpEmpList" class="form-control" asp-for="DesignationID" asp-items="@(new SelectList(Model.DesignationList, "DesignationID", "DesignationName"))"> <option value="">--Select--</option> </select> <input type="hidden" asp-for="DesignationID" /> </div> </div>

NOTE: use the classname, connection and other class members as per your project注意:根据您的项目使用类名、连接和其他 class 成员

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 asp.net core 3.1 mvc 中的另一个表中显示类别名称而不是 categoryId? - How to show category name instead of categoryId in view from another table in asp.net core 3.1 mvc? 如何在另一个表的视图中显示类别名称而不是类别ID? - How to show Category Name instead of Category ID in View from another table? 如何使用正在使用另一个表中数据的MVC中的下拉列表? - how to use a dropdown list in the mvc that is using the data from another table? MVC 5从另一个表填充下拉列表并保存值NOT KEY - MVC 5 populate dropdown list from another table and save value NOT KEY 如何覆盖选择下拉列表的名称和id属性 - How to override the name and id properties of a select dropdown 如何从另一个表中获得与另一个表中的名称或ID匹配的相同名称或ID - How to get same name or id from another table with matching name or id in another table 如何从mvc中的数据库填充下拉列表? 需要在下拉列表中显示数据库中的名称 - How to populate the dropdown list from database in mvc? Need to show names from database in dropdown 如何在mvc5中通过id从两个表中选择 - How to select from two table by id in mvc5 ASP NET MVC中表格的下拉列表 - Dropdown list from table in ASP NET MVC 首先在 ASP.Net MVC 数据库中,当我尝试创建一个将显示另一个表中的值的下拉列表时,它会给出 System.ArgumentNullException - In ASP.Net MVC Database first , when i try to create a dropdown list which will show values from another table , it gives System.ArgumentNullException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM