asp.net mvc - ViewModel Contents are Null after Posting Form to Controller -


so viewmodel has 2 sets of data. currentdetails , updateddetails. both instances of same class carries strings , whatnot inside etc.

this method has worked other views , models i've attempted with, 1 instance, when form posted controller, contents (currentdetails , updateddetails) both found null.

i've tried changing parameter name model test , other arbitrary things, no avail.

the 1 thing worked (but not solution me) not having instances of class inside viewmodel, , having data there (but don't see why should forced things way.

here's controller:

[httppost] public actionresult floristprofile(merchantfloristprofileviewmodel test) {     if (!modelstate.isvalid)         return view(test);      using (var db = new applicationdbcontext())     {         florist florist = db.florists.find(merchantbase.floristid);         if (request.form["editsubmit"] != null)         {             florist.name = test.updateddetails.name;             florist.website = test.updateddetails.website;             db.savechanges();             return redirecttoaction("floristprofile");         }         else if (request.form["photosubmit"] != null)         {             if (test.currentdetails.file.contentlength > 0)             {                 cloudblobcontainer container = flowerstorage.getcloudblobcontainer();                 string blobname = string.format("florist_{0}.jpg", guid.newguid().tostring());                 cloudblockblob photoblob = container.getblockblobreference(blobname);                     photoblob.uploadfromstream(test.currentdetails.file.inputstream);                 florist.logopath = blobname;                 florist.isrendering = true;                 db.savechanges();                 return redirecttoaction("floristprofile");             }         }     }     return content("invalid request"); } 

view:

@using (html.beginform("floristprofile", "merchant", formmethod.post, new { @class = "form-horizontal" })) {     @html.validationsummary(false, "", new { @class = "text-danger" })     @html.hiddenfor(x => x.currentdetails.floristid)     @html.hiddenfor(x => x.currentdetails.name)     @html.hiddenfor(x => x.currentdetails.staffcount)     @html.hiddenfor(x => x.currentdetails.storecount)     @html.hiddenfor(x => x.currentdetails.website)     <div class="form-group">         @html.labelfor(x => x.updateddetails.name, new { @class = "col-sm-2 control-label" })         <div class="col-sm-10">             @html.textboxfor(x => x.updateddetails.name, new { @class = "form-control" })         </div>     </div>     <div class="form-group">         @html.labelfor(x => x.updateddetails.website, new { @class = "col-sm-2 control-label" })         <div class="col-sm-10">             @html.textboxfor(x => x.updateddetails.website, new { @class = "form-control" })         </div>     </div>     <div class="form-group">         <div class="col-sm-offset-2 col-sm-10">             <button type="submit" name="editsubmit" class="btn btn-success">save</button>         </div>     </div> } 

viewmodel:

public class merchantfloristprofileviewmodel {     public class floristprofiledetails     {         public int floristid { get; set; }         [required(errormessage = "please enter name")]         public string name { get; set; }         [datatype(datatype.url)]         [required(errormessage = "please enter website")]         public string website { get; set; }         public int storecount { get; set; }         public int staffcount { get; set; }         // picture upload         public httppostedfilebase file { get; set; }     }     public floristprofiledetails currentdetails;     public floristprofiledetails updateddetails; } 

both currentdetails , updateddetails in merchantfloristprofileviewmodel model fields, not properties (no getter/setter) defaultmodelbinder cannnot set values. change them

public floristprofiledetails currentdetails { get; set; } public floristprofiledetails updateddetails { get; set; } 

but should not sending data view, sending again unchanged. apart overhead, malicious user alter values in hidden fields causing app fail. original repository again if need in post method


Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -