代码之家  ›  专栏  ›  技术社区  ›  MarkKGreenway

ASP.NET编辑器模板下拉列表

  •  9
  • MarkKGreenway  · 技术社区  · 14 年前

    每次我添加一个新的应用程序时,它都会创建一个新的应用程序类别 . 我真的把事情搞砸了

    编码第一实体框架对象

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

    编辑器模板(我只想制作一个外键Editor Template)

    @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();
         }
    
    3 回复  |  直到 14 年前
        1
  •  5
  •   MarkKGreenway    14 年前

    我的模型设置不正确。。。虚拟ICollection和sub的外键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; } 
    }
    

    /视图/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();
        }
    
        2
  •  0
  •   moi_meme    14 年前

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

        3
  •  0
  •   Çağdaş Tekin    14 年前

    模型绑定器无法创建 AppCategory 对象,该对象来自 Create 操作,因为窗体只有该对象的ID(的其他属性 应用程序类别 不在那里)。

    最快的解决方案是设置 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();
     }