Search code examples
asp.net-mvcasp.net-mvc-2editortemplates

ASP.NET EditorTemplate DropdownList


Every time I add a new App It creates a new AppCategory. I am seriously screwing this up somehow

code first entity framework objects

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 (I would love to just make just one Foreign Key EditorTemplate)

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

and of course the repository

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

Every time I add a new App It creates a new AppCategory I am seriously screwing this up somehow


Adding more debug info

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

gives me a validation message on the post

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

Validation error The value '1' is invalid.
This makes sense because Category should be an object not an integer.


Controller Code as asked for pretty sure this isnt the problem as it came from MVCScaffold

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

Solution

  • My model was incorrectly set up ... virtual ICollection and just the foreign key id for the sub and everything worked... changes below

    Model

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