asp.net - upload image in registration form in asp mvc -
i want save model in database , upload picture server want when hit save button
my view model is:
public class postviewmodel { public int postid { get; set; } [required] [stringlength(255)] public string address { get; set; } [required] [stringlength(255)] [display(name = "title")] public string posttitle { get; set; } [required] [display(name = "full text")] public string postcontent { get; set; } [stringlength(30)] [display(name = "image")] public string postimage { get; set; } [display(name = "date")] public datetime createdat { get; set; } public virtual ilist<tagcheckbox> taglist { get; set; } }
and create.cshtml :
@model first.areas.admin.viewmodels.postviewmodel @using (html.beginform("create", "posts", formmethod.post, new { enctype = "multipart/form-data" })) { @html.antiforgerytoken() <div class="row"> <div class="col-lg-8"> <div class="form-horizontal"> @html.validationsummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @html.labelfor(model => model.posttitle, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.textboxfor(model => model.posttitle, new { @class = "form-control" }) @html.validationmessagefor(model => model.posttitle, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @html.labelfor(model => model.address, "لینک ", htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.textboxfor(model => model.address, new { @class = "form-control", data_slug = "#posttitle" }) @html.validationmessagefor(model => model.address, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @html.labelfor(model => model.postcontent, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.textareafor(model => model.postcontent, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.postcontent, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @html.labelfor(model => model.postimage, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> <input type="file" name="fileupload" /> @html.validationmessagefor(model => model.postimage, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="save" class="btn btn-primary" /> </div> </div> </div> </div>
and controller :
public async task<actionresult> create([bind(include = "postid,address,posttitle,postcontent,postimage,taglist")] postviewmodel postviewmodel,httppostedfilebase file) { if (modelstate.isvalid) { string filename = "1.jpg"; if (file != null && file.contentlength > 0) { filename = path.getfilename(file.filename); var path = path.combine(server.mappath("~/content/images/blog"), filename); file.saveas(path); } else { return view(postviewmodel); } var selectedtags = reconsiletags(postviewmodel.taglist); post post = new post { posttitle = postviewmodel.posttitle, postcontent = postviewmodel.postcontent, address = postviewmodel.address, tags = selectedtags }; post.postimage = filename ; post.authorid = user.identity.getuserid(); db.posts.add(post); await db.savechangesasync(); return redirecttoaction("index"); }
the file (httppostedfilebase) null !? please me solve this. (i'm newbie in asp mvc )
instead of passing httppostedfilebase
parameter, remove , following:
httpfilecollectionbase filecollection = request.files; if(filecollection.count > 0) { foreach(var file in filecollection) { // httppostedfilebase object } }
Comments
Post a Comment