c# - MVC Post with complex types, model is empty -
i have following code fill model before posting controller. list i'm iterating through list.
<div class="tab-content"> @*@foreach (var description in model.category.c_categorydescription)*@ @for (var = 0; < model.category.c_categorydescription.count; i++) { <div id="@("tab" + @model.category.c_categorydescription.tolist()[i].producttypeid)" class="@(model.category.c_categorydescription.tolist()[i] == @model.category.c_categorydescription.first() ? "tab-active" : "tab")"> <div class="form-group "> @html.labelfor(model => model.category.c_categorydescription.tolist()[i].descriptiontop, "beskrivelse - top", htmlattributes: new {@class = "control-label col-md-2"}) <div class="col-md-10"> @html.textareafor(model => model.category.c_categorydescription.tolist()[i].descriptiontop, new {@class = "richtext"}) </div> </div> <div class="form-group"> @html.labelfor(model => model.category.c_categorydescription.tolist()[i].descriptionbottom, "beskrivelse - bund", htmlattributes: new {@class = "control-label col-md-2"}) <div class="col-md-10"> @html.textareafor(model => model.category.c_categorydescription.tolist()[i].descriptionbottom, new {@class = "richtext"}) </div> </div> </div> } </div>
the html comes out fine. catch post in controller, model empty. not null, empty. read numerous articles saying points problem model binding.
i changed code reflect what's described: here
still no dice. appreciated.
edit: changed code according this post.
my view looks this:
<div class="tab-content"> @html.partial("_edit", model.category.c_categorydescription.tolist()) </div>
with partial view looking this:
@model ilist<dataaccess.plusbog.c_categorydescription> @{ var producttype = model; } @for (var = 0; < producttype.count; i++) { <div id="@("tab" + @model[i].producttypeid)" class="@(model[i] == @model.first() ? "tab-active" : "tab")"> <div class="form-group "> @html.labelfor(model => producttype[i].descriptiontop, "beskrivelse - top", htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.textareafor(model => producttype[i].descriptiontop, new { @class = "richtext" }) </div> </div> <div class="form-group"> @html.labelfor(model => producttype[i].descriptionbottom, "beskrivelse - bund", htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.textareafor(model => producttype[i].descriptionbottom, new { @class = "richtext" }) </div> </div> </div> }
same result, sadly.
edit:
here's models:
public class categorymodel { public c_category category { get; set; } public selectlist categories { get; set; } public selectlist producttypes { get; set; } public string isbnlisttoaddmanually { get; set; } public string response { get; set; } }
and c_category class:
public partial class c_category { public c_category() { this.c_categorydescription = new hashset<c_categorydescription>(); this.books = new hashset<books>(); this.childcategories = new hashset<c_category>(); this.campaign = new hashset<campaign>(); this.group = new hashset<group>(); } public int id { get; set; } public nullable<int> parentcategoryid { get; set; } public string name { get; set; } public bool active { get; set; } public string slug { get; set; } public string keywords { get; set; } public virtual icollection<c_categorydescription> c_categorydescription { get; set; } public virtual icollection<books> books { get; set; } public virtual icollection<c_category> childcategories { get; set; } public virtual c_category parentcategory { get; set; } public virtual icollection<campaign> campaign { get; set; } public virtual icollection<group> group { get; set; } }
and lastly, c_categorydescription:
public partial class c_categorydescription { public int categoryid { get; set; } public int producttypeid { get; set; } public string descriptiontop { get; set; } public string descriptionbottom { get; set; } public string metadescription { get; set; } public string metakeywords { get; set; } public string alternativetitle { get; set; } public virtual c_category c_category { get; set; } public virtual c_producttype c_producttype { get; set; } }
your code generating elements in collection needs be
@html.textareafor(m => m.category.c_categorydescription[i].descriptiontop, new {@class = "richtext"})
which generate correct name
attributes
<textarea name="category.c_categorydescription[0].descriptionto" ... /> <textarea name="category.c_categorydescription[1].descriptionto" ... />
your current use .tolist()
generating incorrect name
attributes (not tested, assume name="[0].descriptionto"
)
alternatively can use custom editortemplate
c_categorydescription
model if cannot change collection implement ilist
in views/shared/editortemplates/c_categorydescription.cshtml
(note name of file must match name of class)
@model yourassembly.c_categorydescription <div class="form-group "> @html.labelfor(m => m.descriptiontop, "beskrivelse - top", new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.textareafor(m => m.descriptiontop, new { @class = "richtext" }) <div> </div> ....
and in main view, generate correct html each item in collection
@html.editorfor(m => m.category.c_categorydescription)
Comments
Post a Comment