Pak*_*akk 1 asp.net-mvc html-select razor
UserProfiles模型
[Table("Users")]
public class UserProfiles
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
//public string Password { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public string Initials { get; set; }
public string Company { get; set; }
public string Department { get; set; }
public string Title { get; set; }
public string Team { get; set; }
public string TeamSub { get; set; }
public string Phone { get; set; }
public string ImageLocation { get; set; }
public string Note { get; set; }
public string Comment { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
调节器
public ActionResult Index()
{
**EDIT :** ViewBag.ProfileId = new SelectList(db.UserProfiles, "UserName", "UserName");
return View(db.UserProfiles.ToList());
}
Run Code Online (Sandbox Code Playgroud)
在视图中(我希望/相信这是问题)
@model IEnumerable<OilNGasWeb.Models.UserProfiles>
@{
ViewBag.Title = "CLS - Oil & Gas Web Site Users";
}
<h2>Web Site Users</h2>
**Removed** @Html.DropDownList(Model => Model.UserName,Model.UserName)
**Changedinto** @Html.DropDownList("UserName", String.Empty)
Run Code Online (Sandbox Code Playgroud)
新错误:
Exception Details: System.InvalidOperationException: There is no ViewData item of
type 'IEnumerable<SelectListItem>' that has the key 'UserName'.
Run Code Online (Sandbox Code Playgroud)
ViewBag在Controller中添加一个SelectList :
ViewBag.ProfileId = new SelectList(db.UserProfiles, "Id", "Username");
Run Code Online (Sandbox Code Playgroud)
然后在你的视图中添加:
@Html.DropDownList("ProfileId", String.Empty)
Run Code Online (Sandbox Code Playgroud)
此外,您应该在lambda表达式中使用"model"而不是"Model".