while developing asp.net mvc 4 application ran problem updating saved image. when user selects edit tab, re-directed content entered. 1 such item of content image, , when in edit menu can choose replace existing image. have other content updating correctly image remains same, when place breakpoint on "edit" actionresult, show image value passing model null? newsarticle.articleimage = null
fyi - image saved byte array.
am missing ?
controller....
[httppost] public actionresult edit(newsarticle newsarticle, int id, httppostedfilebase article) { try { if (modelstate.isvalid) { newsarticle savedarticle= _newsarticle.get(id); savedarticle.body = newsarticle.body; savedarticle.title = newsarticle.title; if (newsarticle.articleimage == null) { newsarticle.articleimage = savedarticle.articleimage; } else { using (var binaryreader = new binaryreader(request.files[0].inputstream)) { newsarticle.articleimage = binaryreader.readbytes(request.files[0].contentlength); } savedarticle.articleimage = newsarticle.articleimage; } if (newsarticle.imagename == null) { newsarticle.imagename = savedarticle.imagename; } else { string imgename = path.getfilename(article.filename); savedarticle.imagename = imgename; } _uow.savechanges(); return redirecttoaction("index"); } } catch (system.data.dataexception) { //log th error(add variable name after dataexpection) modelstate.addmodelerror("", "unable save changes. try again, , if problem persists see system administrator."); } return view(newsarticle); } view..........
@using (html.beginform("edit", "admin", formmethod.post, new { enctype = "multipart/form-data", @class = "form-horizontal", id = "newseditform" })) { @html.validationsummary() @html.hiddenfor(model => model.id) <div class="control-group"> <label class="control-label">posted on :</label> <div class="controls"> <span class="text">@model.datecreated.value.toshortdatestring()</span> @*@html.labelfor(n => n.datecreated)*@ </div> </div> <div class="control-group"> <label class="control-label">@html.labelfor(n => n.title)</label> <div class="controls"> @html.textboxfor(n => n.title, new { @class = "span4 m-wrap", rows = 1 }) </div> </div> <div class="control-group"> <label class="control-label">@html.labelfor(n => n.body)</label> <div class="controls"> @html.textareafor(n => n.body, new { @class = "span12 ckeditor m-wrap", rows = 4 }) </div> </div> <div class="control-group"> <label class="controls">@html.displaytextfor(model => model.imagename)</label> <div class="span4 blog-img blog-tag-data"> <div class="editor-field"> <input type="file" name="article" id="articleimage" /> </div> </div> </div> <div class="form-actions"> <button type="submit" class="btn green" id="submitnews"><i class="icon-ok"></i>submit</button> @html.actionlink("cancel", "articlelist", "admin", null, new { @class = "btn blue" }) @*<button type="button" class="btn blue" onclick="location.href='articlelist','admin'">cancel</button>*@ </div> }
Comments
Post a Comment