[英]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.