每次添加一个新应用程序时,都会创建一个新的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; }
}

编辑模板(我只想只制作一个外键编辑图板)

@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”无效。
这是有道理的,因为类别应该是一个对象而不是整数。


要求的控制器代码 可以肯定的是,这不是来自MVCSCAFFOLD的问题

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

解决方案

我的模型设置不正确...虚拟偶像,只是该潜艇的外键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())

AppController

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

其他提示

如果将您的下拉列表绑定到类别。ID,则至少您将所选值纳入文件中,但类别对象中没有其他内容。

模型粘合剂无法创建 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();
 }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top