c# - .net MVC; HtmlBegin form displaying error -
i using html.beginform upload files; while validating file if there error how display message user not lose layout style?
code in veiw:
<div id="result"> @html.validationsummary(true) </div> @using (html.beginform("submit", "bulkupload", formmethod.post, new { id = "uploadform", enctype = "multipart/form-data" })) { @(html.telerik().upload() .name("uploadattachment") .multiple(false) ) <input type="submit" id="btnsaveondatabase" value="submit" class="t-button" /> }
code in controller:
{ ... var filename = path.getfilename(file.filename); var extension = path.getextension(file.filename); if (!xlextensions.any(item => item == extension)) { modelstate.addmodelerror("", "not .xls file"); break; } return partialview("~/views/bulkupload.cshtml",uploaddt); }
the validation summary displays refreshes whole page; lose layout specified. suggestions handle welsome..:)
tweaked this... :).. , worked me....
http://www.dustinhorne.com/post/2011/11/16/ajax-file-uploads-with-jquery-and-mvc-3.aspx
public class wrappedjsonresult : jsonresult { public override void executeresult(controllercontext context { context.httpcontext.response.write("<html><body><textarea id=\"jsonresult\" name=\"jsonresult\">"); base.executeresult(context); context.httpcontext.response.write"</textarea></body></html>"); context.httpcontext.response.contenttype = "text/html"; } }
controller code:
if (cond.) { return new wrappedjsonresult { data = new { isvalid = false, message = <error message>, imagepath =string.empty } }; }
in html:
<iframe id="uploadtarget" name="uploadtarget" onload="upload_complete();" style="position: absolute; left: -999em; top: -999em;"></iframe> <div id="images"></div>
jquery:
function uploadimage_complete() { //check see if first load of iframe if (isfirstload == true){ isfirstload = false; return; } //reset image form file won't uploaded again document.getelementbyid("imgform").reset(); //grab content of textarea named jsonresult . //this shold loaded hidden iframe. var newimg = $.parsejson($("#uploadtarget").contents().find("#jsonresult")[0].innerhtml); //if there error, display user if (newimg.isvalid == false) { alert(newimg.message); return; } //create new image , insert images div. //just fancy, we're going use "fadein" effect jquery var imgdiv = document.getelementbyid("images"); var img = new image(); img.src = newimg.imagepath; //hide image before adding dom $(img).hide(); imgdiv.appendchild(img); //now fade image in $(img).fadein(500, null); }
Comments
Post a Comment