im trying upload file, not work expected. have following view:
@using (ajax.beginform("registerband", "newprofile", new ajaxoptions() { httpmethod = "post", insertionmode = insertionmode.replace, }, new { enctype = "multipart/form-data"})) { @html.antiforgerytoken() @html.validationsummary(true, "", new { @class = "text-danger" }) <div class="form-horizontal"> <div class="form-group"> <div class="col-md-10"> bandname </div> <div class="col-md-10"> @html.editorfor(x => x.bandprofile.name, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(x => x.bandprofile.name, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-10"> genres </div> <div class="col-md-10"> @html.dropdownlistfor(x => x.bandprofile.genres, enumerable.empty<selectlistitem>(), new { @class="", multiple = "multiple", style ="width: 100%;"} ) @html.validationmessagefor(x => x.bandprofile.genres, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-10"> coverpicture </div> <div class="col-md-10"> <input type="file" name="file" id="coverpicture" /> @html.validationmessagefor(x => x.bandprofile.coverpicture, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-10"> description </div> <div class="col-md-10"> @html.editorfor(x => x.bandprofile.description, new { htmlattributes = new { @class = "form-control"} }) @html.validationmessagefor(x => x.bandprofile.description, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="spara" class="btn btn-success" /> </div> </div> </div> }
here controller:
[httppost] public actionresult registerband(profileviewmodel model, httppostedfilebase file) { if (modelstate.isvalid == false) { return json(jsonrequestbehavior.allowget); } var bandprofile = _profileservice.createbandprofile(model.bandprofile, file, userid); if (bandprofile != null) { usermanager.addtorole(userid, "band"); return redirecttoaction("index", "welcome"); } return view("index"); }
the problem have file
results in null. can't understand why. can me find problem?
the issue here using ajax.beginform()
helper create , post form. however, files cannot uploaded using ajax.
you may want consider using jquery-based plug-in accomplish this, relies on use of <iframe>
handle uploading operations behind scenes , posting them proper location.
otherwise, consider trying normal form using html.beginform()
, should work in scenario (if don't explicitly need of ajax functionality).
update
another issue here constructor using ajax.beginform()
call accepting ajaxoptions
, htmlattributes
parameter, falls in constructor
however, current use missing third routevalues
parameter. try adding null
in there see if makes difference :
@using(ajax.beginform("registerband", "newprofile", null, new ajaxoptions() { httpmethod = "post", insertionmode = insertionmode.replace }, new { enctype = "multipart/form-data"})){ <!-- content --> }
Comments
Post a Comment