c# - HttpPostedFileBase is Null Every Time -
i saw many questions , searched through other cases on stackoverflow answer why case , none of them applied. can see correct far. name file input tag same variable name on create method in controller. added enctype form. see below:
html:
@using (html.beginform(new { enctype = "multipart/form-data" })) { @html.antiforgerytoken() <div class="form-horizontal"> <p><input type="file" name="file" id="file" /></p> <p><input type="submit" value="update" class="btn btn-default" /></p> </div> }
controller:
[httppost] [validateantiforgerytoken] public actionresult create(httppostedfilebase file) // it's null { if (modelstate.isvalid) { io io = new io(); if (file != null) { updatelog updatelog = io.updateit(file); db.updatelogs.add(updatelog); db.savechanges(); } else { return redirecttoaction("create"); } return redirecttoaction("index"); } return view(); }
i discovered html.beginform method requires 3 arguments in cshtml. had manually specify method , controller.
@using (html.beginform("create", "updatelogs", formmethod.post, new { enctype = "multipart/form-data" }))
Comments
Post a Comment