2010-11-15 159 views
9

每當我添加一個新的應用程序它創建一個新的AppCategory。我很認真地擰緊這在某種程度上ASP.NET EditorTemplate DropdownList

代碼第一個實體框架對象

public class AppCategory 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public ICollection<App> apps { get; set; } 
} 

public class App 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public AppCategory Category { get; set; } 
} 

編輯模板(我很想只是做只有一個外鍵EditorTemplate)

@inherits System.Web.Mvc.WebViewPage 
@Html.DropDownList("Category", LIG2010RedesignMVC3.Models.Repo.GetAppCategoriesSelect()) 

,當然還有庫

public static IEnumerable<SelectListItem> GetAppCategoriesSelect() 
    { 
     return (from p in GetAppCategories() 
       select new SelectListItem 
       { 
        Text = p.Name, 
        Value = p.ID.ToString(), 

       }); 
    } 


    public static ICollection<AppCategory> GetAppCategories() 
    { 
     var context = new LIGDataContext(); 
     return context.AppCategories.ToList(); 
    } 

每當我添加一個新的應用程序它創建一個新的AppC ategory我很認真地擰緊這在某種程度上


增加更多的調試信息

@inherits System.Web.Mvc.WebViewPage 
@Html.DropDownList("", LIG2010RedesignMVC3.Models.Repo.GetAppCategoriesSelect()) 

使我對後

Parameters application/x-www-form-urlencoded 
Category 1 
Name 8 

驗證錯誤值驗證消息 '1'是無效的。
這很有意義,因爲類別應該是一個不是整數的對象。


控制器守則要求 很肯定,因爲它從MVCScaffold

[HttpPost] 
    public ActionResult Create(App d) 
    { 
     if (ModelState.IsValid) 
     { 
      context.Apps.Add(d); 
      context.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 
     return View(); 
    } 
+0

我不知道你的問題是什麼,或者你是怎麼對文章的給予好評。你有沒有在調試器中通過程序來縮小發生問題的地方? – 2010-11-15 20:48:54

+0

從控制器[HttpPost] public ActionResult創建(應用d)我得到d.Category爲空(這就是爲什麼它創建一個新的),但我不知道爲什麼我得到d.Category爲空 – MarkKGreenway 2010-11-15 21:26:30

+0

請發佈您的控制器代碼。我相當確定問題在那裏。 – jfar 2010-11-16 17:07:51

回答

5

我的模型是錯誤地設置了爲子...虛擬ICollection的,只是外鍵ID和一切工作......下面

模式的轉變

public class AppCategory 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public **virtual** ICollection<App> Apps { get; set; } 
} 

public class App 
{ 
    public int ID { get; set; } 
    ******************************************** 
    [UIHint("AppCategory")] 
    public int AppCategoryID { get; set; } 
    ******************************************** 
    public string Name { get; set; } 

} 

public class LIGDataContext : DbContext 
{ 
    public DbSet<AppCategory> AppCategories { get; set; } 
    public DbSet<App> Apps { get; set; } 
} 

/查看/共享/ EditorTemplates/AppCateg ory.cshtml

@inherits System.Web.Mvc.WebViewPage 
@Html.DropDownList("", LIG2010RedesignMVC3.Models.Repo.GetAppCategoriesSelect()) 

AppController的

[HttpPost] 
    public ActionResult Create(App d) 
    { 
     if (ModelState.IsValid) 
     { 
      this.repository.Add(d); 
      this.repository.Save(); 
      return RedirectToAction("Index"); 
     } 
     return View(); 
    } 
0

來到如果你綁定到DROPDOWNLIST這個Category.Id心不是問題,你至少獲得選擇的值到該字段中,但沒有其他類別對象。

+0

它仍然嘗試創建一個新的Category對象 – MarkKGreenway 2010-11-16 17:29:55

0

模型綁定不能在Create行動創建形式收集AppCategory對象,因爲表格只有該對象的ID(的AppCategory的其他屬性不存在)。

最快的解決辦法是,手動設置您App對象的Category財產,像這樣:

[HttpPost] 
public ActionResult Create(App d) { 
    int categoryId = 0; 
    if (!int.TryParse(Request.Form["Category"] ?? String.Empty, out categoryId) { 
     // the posted category ID is not valid 
     ModelState.AddModelError("Category", 
      "Please select a valid app category.") 
    } else { 
     // I'm assuming there's a method to get an AppCategory by ID. 
     AppCategory c = context.GetAppCategory(categoryID); 
     if (c == null) { 
      // couldn't find the AppCategory with the given ID. 
      ModelState.AddModelError("Category", 
       "The selected app category does not exist.") 
     } else { 
      // set the category of the new App. 
      d.Category = c; 
     } 
    } 
    if (ModelState.IsValid) 
    { 
     context.Apps.Add(d); 
     context.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 
    return View(); 
}