How to fill dropdownlist for MVC4 razor views (C#)

前端 未结 1 1332
灰色年华
灰色年华 2021-01-16 08:16

UserProfiles Model

[Table(\"Users\")]
public class UserProfiles
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int         


        
相关标签:
1条回答
  • 2021-01-16 08:58

    Add a SelectList to your ViewBag in the Controller:

    ViewBag.ProfileId = new SelectList(db.UserProfiles, "Id", "Username");
    

    then add this in your View:

    @Html.DropDownList("ProfileId", String.Empty)
    

    Moreover you should have used "model" instead of "Model" in your lambda expression.

    0 讨论(0)
提交回复
热议问题