【发布时间】:2014-11-14 03:40:56
【问题描述】:
我只是想在这个列表中添加一个默认值(“创建新地点”),它可能比我做的更容易。我对方法重载感到困惑,特别是因为我正在使用的重载(由脚手架创建)是 DropDownList(string name, IEnumerable selectList, object htmlAttributes) 并且第二个参数为 null,但它有效。我认为这里有一些约定。谁能阐明这一点和/或如何向此列表添加默认值?
控制器:
ViewBag.VenueId = new SelectList(db.Venues, "Id", "Name", review.VenueId);
return View(review);
}
查看:
<div class="form-group">
@Html.LabelFor(model => model.VenueId, "VenueId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("VenueId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.VenueId, "", new { @class = "text-danger" })
<div>Don't see what you're looking for? Fear not. Just type in the name and create your review; we'll fill in the rest!</div>
</div>
</div>
【问题讨论】:
-
您希望该值是静态添加还是来自模型?
-
使用this overload,其中第三个参数是
default empty item。但是您想将值绑定到属性吗?
标签: c# asp.net-mvc-5