c# - MVC UpdateModel is not updating some of the model properties -
in mvc 5 ef database first project have been using updatemodel method in controller action successfully, after including additional fields (previously unused application) find new fields refuse values updatemodel method call. thing of significance can identify fields share part of name.
consider class example:
public class record { public int id {get;set;} public string details {get;set;} public string detailsfile {get;set;} ... }
the property/field detailsfile
unused, optional field on web form. stored in <input type="hidden" name="detailsfile" id="detailsfile />
, posted controller action correct value (or empty string).
within controller action handle update this:
[httppost] public async task<actionresult> edit(record_editview model, formcollection collection) { if (modelstate.isvalid) { try { var record = await db.record.findasync(model.id); updatemodel(record, collection); db.entry(record).state = entitystate.modified; await db.savechangesasync(); } catch(exception ex) { throw ex; } } // more stuff here ... }
which has been working fine, additional field all fields except detailsfile
updated formcollection passed in. have inspected both model
, collection
, have correct value, record
never gets values until second post of same data. values pushed in fields expected.
i don't errors thrown , @ bit of loss going on. time being have resorted modifying controller action this:
[httppost] public async task<actionresult> edit(record_editview model, formcollection collection) { if (modelstate.isvalid) { try { var record = await db.record.findasync(model.id); updatemodel(record, collection); record.detailsfile = collection["detailsfile"]; // <-- manually insert detailsfile value db.entry(record).state = entitystate.modified; await db.savechangesasync(); } catch(exception ex) { throw ex; } } // more stuff here ... }
and works ok, i'm sure shouldn't have this, , hope out there can explain overlooking!
finally found problem, not benefit many people in case here answer.
more information
in project started using jquery plugin styling file input elements in more bootstrap fashion, namely jasny bootstrap.
the plugin works great, part of it's internal workings takes steps maintain state of existing data , avoid post conflict using hidden input , renaming file input this:
this.$hidden.val('') this.$hidden.attr('name', '') this.$input.attr('name', this.name)
the problem
which ends element(s) have attribute name=""
, caused page post element , formcollection
include empty/"" item(s).
despite fact no errors thrown, nonetheless seems break call to
updatemodel(record,collection)
the solution
to fix , avoid posting selected file (my project doesn't want file posted, file path) intercept form submit remove unwanted form elements prior posting:
$('form').submit(function (e) { e.preventdefault(); // rid of input elements have name="" or file elemnts not mess posted form collection $('input[name=""],input[type="file"]').remove(); this.submit(); })
Comments
Post a Comment