c# - Controller action not grabbing file from view when attempting to upload -
i trying upload .csv file sql database, controller action not seem grabbing file @ all. check out code, must missing piece:
view:
@using (html.beginform("uploadvalidationtable", "home", formmethod.post)) { <table style="margin-top: 150px;"> <tr> <td> <label for="csvfile">filename:</label> </td> <td> <input type="file" name="csvfile" id="csvfile"/> </td> <td><input type="submit" value="upload"/></td> </tr> </table> }
controller:
[httppost] public jsonresult uploadvalidationtable(httppostedfilebase csvfile) { var inputfiledescription = new csvfiledescription { separatorchar = ',', firstlinehascolumnnames = true }; var cc = new csvcontext(); var filepath = uploadfile(csvfile.inputstream); var model = cc.read<outstandingcreditcsv>(filepath, inputfiledescription); try { var entity = new outstandingcreditcsv(); foreach (var item in model) { entity.ponumber = item.ponumber; entity.creditinvoicedate = item.creditinvoicedate; entity.creditinvoicenumber = item.creditinvoicenumber; entity.creditinvoiceamount = item.creditinvoiceamount; } } catch(linqtocsvexception ex) { } return json(model, "text/json"); }
csvfile appearing null, no clue going on since named in view , have post method surrounding it. makes down until var filepath = uploadfile(csvfile.inputstream);
, breaks since method trying pass null value. thanks!
i came across when trying upload files db using mvc while back, using scaffolded views (so passed object form), might bit different.
in html.beginform() call you'll need add attribute enctype="multipart/form-data"
@using (html.beginform("uploadvalidationtable", "home", formmethod.post, new {enctype="multipart/form-data"}))
Comments
Post a Comment