c# - How to update just some of properties in Edit action -
preface:
i'm working on project using mvc 5. we've built our database
our model
created. then, we've added controllers
, views
using scaffolding. in views we've omitted of properties (they should not shown users). problem: in actions edit, nail save button (to update model) encounter exception
, think requires me provide value of properties.
please allow me know how can update of properties (shown in view)? please note need solution general possible, able utilize in many edit actions have in project (indeed hard part). code: next of codes think related question:
[httppost] [validateantiforgerytoken] public actionresult edit([bind(include = "areacode,tels,address")] area area) {//area has many more properties (defined required in database) , need update //these : areacode,tels,address if (modelstate.isvalid) { db.entry(area).state = entitystate.modified; db.savechanges(); homecoming redirecttoaction("index"); } viewbag.detector = new selectlist(db.detectors, "detectorcode", "detectorname", area.detector); viewbag.grade = new selectlist(db.grades, "gradeid", "gradename", area.grade); homecoming view(area); }
ss:
answers expressed in simple way highly appreciated.
a couple of alternatives
in view, create hidden inputs properties don't want displayed e.g. @html.hiddenfor(m => m.some property), posted , area populated (the bindattribute not necessary). there risk malicious user modify values of hidden inputs.create separate view model contains properties want edit including objects id property (could include properties adding viewbag) , in action method
public actionresult edit(areavm model) { area area = db.get(...// object database area.address = model.address ///= update properties view model
c# asp.net asp.net-mvc database asp.net-mvc-scaffolding
No comments:
Post a Comment