最后一天,我在 Razor View 中看到了这一点
@Html.DropDownListFor(x => x.FoodId,null, htmlAttributes: new { @class = "form-control" })
// and (just another alternative)
@Html.DropDownList("FoodId", null, htmlAttributes: new { @class = "form-control" })
我想知道列表中的数据发生了什么,数据填充发生在何时何地?两条线有什么区别?以及下拉列表如何填充数据(因为代码执行中有数据)
抱歉我的英语不好
更新#1
型号: Food.cs 和 User.cs
public partial class Food
{
public Food()
{
this.User = new HashSet<User>();
}
public System.Guid Id { get; set; }
public string Name { get; set; }
public virtual ICollection<User> User { get; set; }
}
//
public partial class User
{
public System.Guid Id { get; set; }
public string Name { get; set; }
public Nullable<System.Guid> FoodId { get; set; }
public virtual Food Food { get; set; }
}
Controller (仅操作)
public ActionResult Create()
{
ViewBag.FoodId = new SelectList(db.FoodSet, "Id", "Name");
return View();
}
天哪,谢谢,我现在很愚蠢:)
答案:充满 ViewBag.FoodId
最佳答案
当将 null
值传递给第二个参数时,它会自动在 ViewBag
中查找,即使没有 JavaScript 且已清除缓存> 与下拉菜单同名的键,否则启动异常。
就我而言,传递了 null
并检查了 ViewBag
并找到了一个 key
关于c# - 下拉列表中的空值(for),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29987288/