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:
- scaffolded full controller person index, create, edit...
- created editortemplates folder in views->person
created person.cshtml:
@model testeditortemplate.models.person <div> <h4>child</h4> @html.textboxfor(s => s.name) @html.hiddenfor(s => s.id) </div>
added
@html.editorfor(m => m.children)
create.cshtml
questions:
- how can
@html.editorfor(m => m.children)
possibly work editor template whenm.children
collection ofperson
, not singleperson
? - 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
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.
one important thing remember is, editor template view's name should same type binding it.
Comments
Post a Comment