2015-05-01 16 views
2

最後一天,我看到在我的Razor視圖空值在dropdownllist(對)

@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; } 
} 

控制器(僅動作)

public ActionResult Create() 
    { 
     ViewBag.FoodId = new SelectList(db.FoodSet, "Id", "Name"); 
     return View(); 
    } 

OMG感謝我現在愚蠢的:)

了充滿ViewBag.FoodId

+2

''