9

每次我添加一个新的 App 它都会创建一个新的 AppCategory。我以某种方式认真地搞砸了

代码优先实体框架对象

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();
    }

每次我添加一个新的应用程序它都会创建一个新的 AppCategory 我以某种方式严重搞砸了


添加更多调试信息

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

在帖子上给我一条验证信息

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

验证错误值“1”无效。
这是有道理的,因为 Category 应该是一个对象而不是整数。


控制器代码 很确定这不是问题,因为它来自 MVCScaffold

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

3 回答 3

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

/Views/Shared/EditorTemplates/AppCategory.cshtml

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

应用控制器

 [HttpPost]
    public ActionResult Create(App d)
    {
        if (ModelState.IsValid)
        {
          this.repository.Add(d);
          this.repository.Save();
          return RedirectToAction("Index");  
        }
        return View();
    }
于 2010-11-19T16:03:17.557 回答
0

如果您将 dropDownList 绑定到 Category.Id,您至少会将所选值放入该字段,但不会在您的 Category 对象中获得其他值。

于 2010-11-16T16:58:40.373 回答
0

模型绑定器无法AppCategory在您的操作中从表单集合创建对象,Create因为表单只有该对象的 ID(AppCategory不存在其他属性)。

最快的解决方案是手动设置对象的Category属性,如下所示:App

[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();
 }
于 2010-11-19T04:53:56.020 回答