ASP.NET EditorTemplate DropdownList -
every time add new app creates new appcategory. screwing 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 love make 1 foreign key editortemplate)
@inherits system.web.mvc.webviewpage @html.dropdownlist("category", lig2010redesignmvc3.models.repo.getappcategoriesselect())
and of course 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 add new app creates new appcategory screwing somehow
adding more debug info
@inherits system.web.mvc.webviewpage @html.dropdownlist("", lig2010redesignmvc3.models.repo.getappcategoriesselect())
gives me validation message on post
parameters application/x-www-form-urlencoded category 1 name 8
validation error the value '1' invalid.
makes sense because category should object not integer.
controller code asked for pretty sure isnt problem came mvcscaffold
[httppost] public actionresult create(app d) { if (modelstate.isvalid) { context.apps.add(d); context.savechanges(); return redirecttoaction("index"); } return view(); }
my model incorrectly set ... virtual icollection , foreign key id sub , 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(); }
Comments
Post a Comment