我有一个不断变化的行业名单,我希望用户选择时,创建一个新的调查。
我可以通过ViewModel或ViewBag来实现这一点(我认为)。我正试图用ViewBag来做这件事。获取DropDownListFor错误:
CS1928
HtmlHelper<Survey>不包含DropDownListFor的定义,最好的扩展方法重载SelectExtensions.DropDownListFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, IEnumerable<SelectListItem>, object)有一些无效的参数2_Views_Surveys_Create.cshtml。
调查模式,带有外键的工业:
public class Survey
{
[Key]
public int id { get; set; }
[Display(Name = "Survey Name")]
public string name { get; set; }
[Display(Name = "Industry")]
public int industryId { get; set; }
[ForeignKey("industryId")]
public virtual Industry industry { get; set; }
}将工业SelectList加载到ViewBag的控制器:
// GET: Surveys/Create
public ActionResult Create()
{
ViewBag.Industries = new SelectList(db.industry, "Id", "Name");
return View();
}创建视图:
<div class="form-group">
@Html.LabelFor(model => model.industryId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.industryId, ViewBag.Industries, "-Select Industry-")
@Html.ValidationMessageFor(model => model.industryId, "", new { @class = "text-danger" })
</div>
</div>发布于 2018-09-10 20:27:46
ViewBag的属性没有编译器可以用来决定要调用的方法的重载的类型。通过使用显式强制转换帮助编译器。
@Html.DropDownListFor(model => model.industryId, (IEnumerable<SelectListItem>)ViewBag.Industries, "-Select Industry-")https://stackoverflow.com/questions/52264750
复制相似问题