c# - null value after reading from database in mvc -


i have edit view , edit action in blog controller. after created post "create action" , after upload image database folder, update path on post.postimage (string value). can see image in folder, , can see path of image , can see preview of picture in edit view. in database saved (~/images/postid/picturename). after edit post, want make checkbox if checked can edit picture , when not checked delete picture. send parameters, , problem on debugger see "string postimage" null on database table has path! , because of of doesn't work, don't care logic, why null???? here code:

view:

@model webapplication1.models.post  @{     viewbag.title = "edit";     layout = "~/views/shared/_layout.cshtml"; }  <h2>edit</h2>   @using (html.beginform(html.beginform("edit", "blog", formmethod.post, new { enctype = "multipart/form-data" }))) {     @html.antiforgerytoken()      <div class="form-horizontal">         <h4>post</h4>         <hr />         @html.validationsummary(true, "", new { @class = "text-danger" })         @html.hiddenfor(model => model.postid)          <div class="form-group">             @html.labelfor(model => model.posttitle, htmlattributes: new { @class = "control-label col-md-2" })             <div class="col-md-10">                 @html.editorfor(model => model.posttitle, new { htmlattributes = new { @class = "form-control" } })                 @html.validationmessagefor(model => model.posttitle, "", new { @class = "text-danger" })             </div>         </div>          <div class="form-group">             @html.labelfor(model => model.postauthor, htmlattributes: new { @class = "control-label col-md-2" })             <div class="col-md-10">                 @html.editorfor(model => model.postauthor, new { htmlattributes = new { @class = "form-control" } })                 @html.validationmessagefor(model => model.postauthor, "", new { @class = "text-danger" })             </div>         </div>          <div class="form-group">             @html.labelfor(model => model.website, htmlattributes: new { @class = "control-label col-md-2" })             <div class="col-md-10">                 @html.editorfor(model => model.website, new { htmlattributes = new { @class = "form-control" } })                 @html.validationmessagefor(model => model.website, "", new { @class = "text-danger" })             </div>         </div>          <div class="form-group">             @html.labelfor(model => model.postdate, htmlattributes: new { @class = "control-label col-md-2" })             <div class="col-md-10">                 @html.editorfor(model => model.postdate, new { htmlattributes = new { @class = "form-control" } })                 @html.validationmessagefor(model => model.postdate, "", new { @class = "text-danger" })             </div>         </div>          <div class="form-group">             @html.labelfor(model => model.posttext, htmlattributes: new { @class = "control-label col-md-2" })             <div class="col-md-10">                 @html.editorfor(model => model.posttext, new { htmlattributes = new { @class = "form-control" } })                 @html.validationmessagefor(model => model.posttext, "", new { @class = "text-danger" })             </div>         </div>          <div class="form-group">               <div>                 <b>upload image:</b>                 @if (!model.postimage.isempty())                 {                                             @html.checkbox("checkimage", true)                         <img src="@url.content(model.postimage)" alt="@model.postauthor" width="300" />                 }                  else                 {                     @html.checkbox("checkimage", false)                 }             </div>                         <input type="file" name="file" id="file" />             <!-- show message of controller -->             @viewbag.message         </div>              <div class="form-group">             @html.labelfor(model => model.postvideo, htmlattributes: new { @class = "control-label col-md-2" })             <div class="col-md-10">                 @html.editorfor(model => model.postvideo, new { htmlattributes = new { @class = "form-control" } })                 @html.validationmessagefor(model => model.postvideo, "", new { @class = "text-danger" })             </div>         </div>          <div class="form-group">             <div class="col-md-offset-2 col-md-10">                 <input type="submit" value="save" class="btn btn-default" />             </div>         </div>     </div> }  <div>     @html.actionlink("back posts list", "index") </div>  @section scripts {     @scripts.render("~/bundles/jqueryval") } 

edit action in blog controller:

   // get: blog/edit/5         public actionresult edit(int? id)         {             if (id == null)             {                 return new httpstatuscoderesult(httpstatuscode.badrequest);             }             post post = db.posts.find(id);             if (post == null)             {                 return httpnotfound();             }             return view(post);         }          // post: blog/edit/5         // protect overposting attacks, please enable specific properties want bind to,          // more details see http://go.microsoft.com/fwlink/?linkid=317598.         [httppost]         [validateantiforgerytoken]         public actionresult edit([bind(include = "postid,posttitle,postauthor,website,postdate,posttext,postimage,postvideo")] post post, httppostedfilebase file, bool checkimage)         {             var filename = "";             if (modelstate.isvalid)             {                 db.entry(post).state = entitystate.modified;                  if (checkimage == true)                 {                     //check if there file                     if (file != null && file.contentlength > 0)                     {                         //check if there image                         var supportedtypes = new[] { "jpg", "jpeg", "gif", "png" };                         var fileext = system.io.path.getextension(file.filename).substring(1);                          if (!supportedtypes.contains(fileext))                         {                             viewbag.message = "invalid image type. following types (jpg, jpeg, gif, png) supported";                             return view();                         }                          //check if there file on database                         if ( !(string.isnullorempty(post.postimage)) )                         {                                                         //delete old file in folder                                                                                     system.io.file.delete(post.postimage);                              //save new file in folder                             var folder = path.combine(server.mappath("~/images/"), convert.tostring(post.postid));                             var path = path.combine(folder, filename);                             file.saveas(path);                              //save path in database                             string targetpath = string.concat("~/images/", convert.tostring(post.postid), "/", filename);                             post.postimage = targetpath;                         }                          //no file in database                         else                         {                             var folder = path.combine(server.mappath("~/images/"), convert.tostring(post.postid));                             var path = path.combine(folder, filename);                             file.saveas(path);                              //save path in database                             string targetpath = string.concat("~/images/", convert.tostring(post.postid), "/", filename);                             post.postimage = targetpath;                         }                     }                      //checkbox checked not file uploaded                     else                         viewbag.message = "checkbox checked, please upload image";                     return view();                 }                  else                 {                     //checkbox not checked - delete image database                     if( !(string.isnullorempty(post.postimage)) )                     {                         //delete old file in folder                                                                             try                         {                             system.io.file.delete("\a.txt");                             post.postimage = null;                         }                         catch (system.io.ioexception e)                         {                             console.writeline(e.message);                                                         }                                                                                             }                      db.savechanges();                     return redirecttoaction("index");                 }             }             return view(post);         } 

render postimage field

@html.editorfor(model => model.postimage, new { htmlattributes = new { @class = "form-control" } }) 

Comments