asp.net mvc - Is there a better way to add a child record using MVC 4 and Entity Framework 5? -
i learning mvc , dealing stateless nature in combination entity framework. question is, there more elegant way of handling scenario below?
i have 2 poco entities:
public class contest { public long id { get; set; } .... ..... public icollection<contestprize> prizes { get; set; } } public class contestprize { public long id { get; set; } public contest contest { get; set; } }
i have mvc view displays contest , prizes associated them. on view have "create prize" link create new prize passes id of contest contestprize controller so:
@html.actionlink("create prize", "create", "contestprizes", new { contestid = model.id }, null)
the contestid persisted hidden field in form submits new prize can retrieve on create. create method on prize controller below:
[httppost] public actionresult create(int parentid, contestprize contestprize) { if (modelstate.isvalid) { db.contestprizes.add(contestprize); db.savechanges(); return redirecttoaction("index", "contests", parentid); } return view(contestprize); }
this run ugliness don't like. here problems:
- the
modelstate.isvalid
test fails. because navigation property parent null on model. hasn't been populated. can skip validation on model , go right database there way populate without doing #2 below? - the parent property isn't populated @ point, have parentid comes in hidden form field. since stateless, db object doesn't have parent object (which fine). so, perform database read parent object using parentid ,
parent.prizes.add(contestprize)
orcontestprize.contest = parent
. don't because making database hit read parent, when have information need create child record.
so, there way entity framework set id of parent , save child without having retrieve parent object? or...ideally there way using mvc pre-populate new model on create parent object since have parent before heading create views?? how bad pass around parent object in viewdata maybe available?
i can make work reloading parent. want avoid if possible.
thoughts?
update: adding contestid
contestprize poco , decorating navigation property [foreignkey("contestid")]
@nsgaga suggested works , eliminates of messy parentid passing around problems. new pocos looks this:
public class contest { public long id { get; set; } .... ..... public icollection<contestprize> prizes { get; set; } } public class contestprize { public long id { get; set; } public long contestid { get; set; } [foreignkey("contestid")] public contest contest { get; set; } }
i can eliminate parentid being passed , set in views hidden field because data binding in mvc takes care of contestid on new entity (i still have pass id initial view, all. bound there on out). new create action on controller looks this:
[httppost] public actionresult create(contestprize contestprize) { if (modelstate.isvalid) { db.contestprizes.add(contestprize); db.savechanges(); return redirecttoaction("index", "contests", contestprize.contestid); } return view(contestprize); }
clean , simple no mess. it.
if you're using 'code first' (all things suggest), can like...
public class contestprize { public long id { get; set; } public long contestid { get; set; } public contest contest { get; set; } }
that should create (by convention) , map contestid fk. in complicated cases may need specify relationship in fluent code, should suffice normally.
then can reference parent
contestid
- fill in - ,add
.
that's in rough strokes, let me know if need more info.
Comments
Post a Comment