Friday, 15 April 2011

asp.net mvc - MVC4 how to save changes of only a select field without affecting others -



asp.net mvc - MVC4 how to save changes of only a select field without affecting others -

i have edit page users can update 2 fields city , state, whole model has 11 different fields again; allow them edit 2 plus 1 other uniqueid can't edit that. issue upon editing , saving new info other fields become null in database particular row, can causing this? code

[authorize] public actionresult edit() { //get user uniqueid var ss = convert.toint32(user.identity.name); // uniqueid match in database , select 3 fields profileid,city,state string connectionstring = configurationmanager.connectionstrings ["looglercontext"].connectionstring; using (system.data.sqlclient.sqlconnection sqlconnection = new system.data.sqlclient.sqlconnection(connectionstring)) { sqlconnection.open(); var getinfo = sqlconnection.query<profile>("select profileid,city,state profiles profileid=@myprofile", new { myprofile = ss }).firstordefault(); homecoming view(getinfo); } }

my view looks this

@model hackerway.models.profile using (html.beginform("edit", "profile", formmethod.post { @html.hiddenfor(model => model.profileid) @html.editorfor(model => model.city) @html.editorfor(model => model.state) <div style="margin-left: 200px"> <p class="name"> <input type="submit" name="myedit" value="update" /> </p> </div> }

after user clicks update button go simple httppost

[httppost] public actionresult edit(profile profiler) { // utilize profiler fields view , update them if (modelstate.isvalid) { db.entry(profiler).state = entitystate.modified; db.savechanges(); viewbag.success = "your changes have been saved"; homecoming view(profiler); } }

as stated before works if field gets updated changes saved other 11 fields i'm nut putting in form homecoming 'null'. in sql code grabbing fields need hence reason didn't utilize * help great

you're understanding of how info moves , forth between database isn't quite right. selecting fields need database absolutely correct; there's nil wrong that.

where code needs alter in post method. method takes in profile typed parameter. when info html form submitted server, inputs form passed server (i.e. profileid, city , state). other properties on profiler object null because how else asp.net supposed know are? user passed on 3 values.

in entity framework world way solve following:

public actionresult edit(profile profiler) { if (modelstate.isvalid) { //go fetch existing profile database var currentprofile = db.profiles.firstordefault(p => p.profileid == profiler.profileid); //update database record values model currentprofile.city = profiler.city; currentprofile.state = profiler.state; //commit database! db.savechanges(); viewbag.success = "your changes have been saved"; homecoming view(profiler); } }

to give finish answer, there is way solve issue. however, highly recommend don't , i'll explain why shortly.

the other way solve issue add together additional hidden inputs on form other fields in table. doing so, when user submits form asp.net's model binding process create sure other properties on profiler object have data. when commit object database, @ point have need.

why bad idea? let's load page , want seek changing info i'm not supposed to. modify values of hidden inputs, submit form, , you're application commit info database! bad news bears. using hidden inputs putting trust in user. in cases may acceptable risk (e.g. little web app co-worker), in other cases high risk or fraudulent.

it adds unnecessary overhead in terms of bytes traveling on wire, that's minor detail in comparing latter.

asp.net-mvc asp.net-mvc-4

No comments:

Post a Comment