asp.net mvc - MVC - Create object and related objects in one go -


i want create parent object child/related objects in same view. example be: create 1 father (with name) along sons (with names). have created view model:

public class fatherviewmodel {   public father father {get; set;} // has 1 property name   public list<son> {get; set;} // has 1 property name } 

my question is, how list of sons view when post performed? have tried using hiddenfor each son id, no matter what, list empty when returned controller.

update:

i tried editor template example shyju described below, editor never called. have 1 object:

public class person {     public int id { get; set; }     public string name { get; set; }     public int? fatherid { get; set; }     public virtual icollection<person> children { get; set; } } 

i did this:

  1. scaffolded full controller person index, create, edit...
  2. created editortemplates folder in views->person
  3. created person.cshtml:

    @model testeditortemplate.models.person <div> <h4>child</h4> @html.textboxfor(s => s.name) @html.hiddenfor(s => s.id) </div>

  4. added @html.editorfor(m => m.children) create.cshtml

questions:

  1. how can @html.editorfor(m => m.children)possibly work editor template when m.children collection of person , not single person?
  2. i want create (not edit) father including children @ same time. means have no ids pass create view start with. how can work? example shyju, ids created beforehand?? or did misunderstand example?

you can use editortemplates handle this. here working sample.

so have viewmodel represent father-child relationship

public class personvm {     public int id { set; get; }     public string name { set; get; }     public int? parentid { set; get; }     public list<personvm> childs { set; get; } } 

and in action method, create object of view model , load father -childs data it.

public actionresult editortmp(int id = 1) {     //hard coded demo, may replace actual db values     var person = new personvm {id = 1, name = "mike"};     person.childs = new list<personvm>     {         new personvm {id = 2, name = "scott", parentid = 11},         new personvm {id = 2, name = "gavin", parentid = 12}     };     return view(person); } 

now create editortemplate. that, go views folder, , create directory called editortemplates under directory has same name controller, , add view name personvm.cshtml

enter image description here

now, go view , add below code.

@model replacewithyournamespacenamehere.personvm <div>     <h4>childs </h4>     @html.textboxfor(s => s.name)     @html.hiddenfor(s => s.id) </div> 

now let's go our main view. need make view typed our original personvm. use editorfor html helper method in view call our editor template

@model replacewithyournamespacenamehere.personvm @using (html.beginform()) {     <div>         @html.textboxfor(s => s.name)         @html.hiddenfor(s => s.id)     </div>     @html.editorfor(s=>s.childs)    <input type="submit"/> } 

now have httppost method in controller handle form posting

[httppost] public actionresult editortmp(personvm model) {     int fatherid = model.id;     foreach (var person in model.childs)     {         var id=person.id;         var name = person.name;     }     //  : save ,then redirect (prg pattern)     return view(model); } 

now, if put break point in httppost action method, can see id's of childs passed action method.

enter image description here

one important thing remember is, editor template view's name should same type binding it.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -