asp.net mvc - jQuery event becoming unattached on load of partial view -
i using jquery fileupload plug-in (https://github.com/blueimp/jquery-file-upload) mvc4 app.
the file upload functionality (upload , table of uploaded files) contained in partial view.
so have number of views contain file upload partial view. when file uploaded, save action of attachment controller fired. handles storing of file , retrieves updated list of files particular area of site. view returned fileupload jquery , injects html div in parent page (#_attachments).
all works fine in renders correctly. issue having after file upload performed , partial view reloaded via jquery, fileupload no longer works.
it appears may due event no longer being attached #fileupload control. have tried using 'on' method doesn't seem work either.
partial view script
$(function () { $('#fileupload').fileupload({ url: "/attachment/save", done: function (e, data) { // "data.result" contain response data $("#fileuploadprogress").hide(); $("#_attachments").html(data.result); }, progressall: function (e, data) { var progress = parseint(data.loaded / data.total * 100, 10); $("#fileuploadprogress").show(); $("#fileuploadprogress .bar").css("width", progress + "%"); } }); });
controller/action
[httppost] public actionresult save() { // reference file our jquery sent. multiple files, own request , 0 index httppostedfilebase file = httpcontext.request.files[0]; int ncpid = convert.toint32(request.form["ncpid"]); int stage = convert.toint32(request.form["stage"]); ncprepository.saveattachmenttodb(file, currentuser.userid, ncpid, stage); //return partial view refresh list of files var attachments = ncprepository.getattachmentsforrecord(ncpid); var attachmentsviewmodel = automapper.mapper.map<iqueryable<attachment>, list<attachmentviewmodel>>(attachments); viewdata["stage"] = stage; return partialview("_stageattachments", attachmentsviewmodel); }
it appears may due event no longer being attached #fileupload control.
yes, that's looks like. there couple of issues code. first issue mentioned javascript code inside partial view. partial views should not contain scripts. javascripts belong separate files. seem have used $('#fileupload')
selector id selector. said have many of partial views. potentially having broken dom because can have 1 element specified id within entire html.
so let's start fixing situation moving script separate file (referenced main view once) , reattach fileupload control new elements in dom:
var attachfileuploads = function() { $('.fileupload').fileupload({ url: "/attachment/save", // todo: never hardcode url that, read html 5 data-* attribute on corresponding file input such data-url="@url.action("save", "attachment")" done: function (e, data) { $("#fileuploadprogress").hide(); $("#_attachments").html(data.result); attachfileuploads(); // <!-- reattach fileupload plugin }, progressall: function (e, data) { var progress = parseint(data.loaded / data.total * 100, 10); $("#fileuploadprogress").show(); $("#fileuploadprogress .bar").css("width", progress + "%"); } }); }; $(attachfileuploads);
in example have used class selector $('.fileupload')
assumes can have more 1 file input. make sure have assigned class , have gotten rid of id
must unique mentioned earlier.
Comments
Post a Comment