2010-11-15 60 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(); 
}