C# ';HtmlHelper<;游戏>';不包含';DropDownListFor';

C# ';HtmlHelper<;游戏>';不包含';DropDownListFor';,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,出于某种原因,DropDownListFor对我不起作用,我也找不到原因 我的游戏模型: public class Game { public virtual int GameId { get; set; } public virtual string Name { get; set; } public virtual Studio Studio { get; set; } public virtual Genre Genre { get; set; }

出于某种原因,
DropDownListFor
对我不起作用,我也找不到原因

我的游戏模型:

public class Game
{
    public virtual int GameId { get; set; }
    public virtual string Name { get; set; }
    public virtual Studio Studio { get; set; }
    public virtual Genre Genre { get; set; }
    public virtual List<Level> Levels { get; set; }
}
我的看法是:

@using GameStore.Controllers
@using GameStore.Models
@model GameStore.Models.Game

@using (Html.BeginForm())
{
        <div class="form-group">
            @Html.LabelFor(model => model.Genre, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(m=>m.Genre, ViewBag.GenreList, "GenreId", "name")
                @Html.ValidationMessageFor(model => model.Genre, "", new { @class = "text-danger" })
            </div>
        </div>
}
@使用GameStore.Controllers
@使用GameStore.Models
@模型游戏商店
@使用(Html.BeginForm())
{
@LabelFor(model=>model.Genre,htmlAttributes:new{@class=“controllabel col-md-2”})
@DropDownListFor(m=>m.Genre,ViewBag.GenreList,“GenreId”,“name”)
@Html.ValidationMessageFor(model=>model.Genre,“,new{@class=“text danger”})
}

视图甚至没有加载,我从这篇文章的主题中得到了错误。当我为lambda代码编写
dropdownlist时,intelissense不适用于
m=>m.Genre
。我不知道我做错了什么,我迷路了。我在谷歌上搜索了一下,什么也没找到

我展示了一种填充
DropDownList
的方法,几乎与您的方法相似:

型号

public class Department
{
    public int DepartmentID { get; set; }
    public string Code { get; set; }
}
 public ActionResult Index()
 {
    MainDbContext db = new MainDbContext();
    var departments = (from c in db.Departments
                       select new Department
                       {
                          DepartmentID = c.Id,
                          Code = c.Code
                       }).ToList(); //Get department details

    return View(departments);
 }
控制器

public class Department
{
    public int DepartmentID { get; set; }
    public string Code { get; set; }
}
 public ActionResult Index()
 {
    MainDbContext db = new MainDbContext();
    var departments = (from c in db.Departments
                       select new Department
                       {
                          DepartmentID = c.Id,
                          Code = c.Code
                       }).ToList(); //Get department details

    return View(departments);
 }
视图-在视图中,使用以下命令:

@model List<YourAppName.Models.Department>
@{
    ViewBag.Title = "SampleApp";
}

<select name="Department" id="Departments" class="form-control">
  <option value="0">--Please Select Department--</option>
      @foreach (var item in Model) //Loop through the department to get department details
      {
        <option value="@item.DepartmentID">@item.Code</option>
      }
</select>
@型号列表
@{
ViewBag.Title=“SampleApp”;
}
--请选择部门--
@foreach(模型中的var项)//遍历部门以获取部门详细信息
{
@项目.代码
}

提示:尽量不要使用
ViewBag
,以上内容仅供演示之用。相反,试着使用
ViewModel

为什么用ViewBag演示一个示例,然后只告诉我们不应该使用ViewBag?我相信,以上就是您要求@Wazner的内容。