Upload image in ASP.NET MVC 5 EF6 -
i've had @ other questions , tutorials, i'm struggling incorporate suggestions existing work.
i want user able upload image in submission form new listing, listing model looks this;
public class listing { public int listingid { get; set; } [display(name = "select category")] public int categoryid { get; set; } [display(name = "select vendor")] public int vendorid { get; set; } [required] [display(name = "listing name")] public string listingname { get; set; } [required] [display(name = "listing description")] public string listingdesc { get; set; } [required] [display(name = "maximum capacity")] public int capacity { get; set; } [required] [datatype(datatype.currency)] [display(name = "price")] public decimal price { get; set; } [required] [display(name = "fixed price?")] public bool isfixedprice { get; set; } [required] public string address { get; set; } [display(name = "address line 2")] public string address2 { get; set; } [required] public string city { get; set; } [required] public string postcode { get; set; } public virtual category category { get; set; } public virtual vendor vendor { get; set; } }
i have separate controller, create method follows;
// get: listing/create public actionresult create() { viewbag.categoryid = new selectlist(db.categories, "categoryid", "categoryname"); viewbag.vendorid = new selectlist(db.vendors, "id", "companyname"); return view(); } // post: listing/create // protect overposting attacks, please enable specific properties want bind to, // more details see https://go.microsoft.com/fwlink/?linkid=317598. [httppost] [validateantiforgerytoken] public actionresult create([bind(include = "listingid,categoryid,vendorid,listingname,listingdesc,capacity,price,isfixedprice,address,address2,city,postcode")] listing listing) { if (modelstate.isvalid) { db.listings.add(listing); db.savechanges(); return redirecttoaction("index"); } viewbag.categoryid = new selectlist(db.categories, "categoryid", "categoryname", listing.categoryid); viewbag.vendorid = new selectlist(db.vendors, "id", "companyname", listing.vendorid); return view(listing); }
any direction on how can implement seemingly simple task existing code appreciated!
i found solution works allows me upload single file uploads directory.
my model has remained unchanged.
the controller updated include;
httppostedfilebase file
and;
if (file.contentlength > 0) { string filename = path.getfilename(file.filename); string directory = server.mappath("~/content/uploads/"); if (!directory.exists(directory)) { directory.createdirectory(directory); } string path = path.combine(directory, filename); file.saveas(path); }
which in context of original code forms this;
// post: listing/create // protect overposting attacks, please enable specific properties want bind to, // more details see https://go.microsoft.com/fwlink/?linkid=317598. [httppost] [validateantiforgerytoken] public actionresult create([bind(include = "listingid,categoryid,vendorid,listingname,listingdesc,capacity,price,isfixedprice,address,address2,city,postcode")] listing listing, httppostedfilebase file) { if (file.contentlength > 0) { string filename = path.getfilename(file.filename); string directory = server.mappath("~/content/uploads/"); if (!directory.exists(directory)) { directory.createdirectory(directory); } string path = path.combine(directory, filename); file.saveas(path); //var filename = path.getfilename(file.filename); //var path = path.combine(server.mappath("~/content/uploads")); //file.saveas(path); } if (modelstate.isvalid) { db.listings.add(listing); db.savechanges(); return redirecttoaction("index"); } viewbag.categoryid = new selectlist(db.categories, "categoryid", "categoryname", listing.categoryid); viewbag.vendorid = new selectlist(db.vendors, "id", "companyname", listing.vendorid); return view(listing); }
in create view, added;
@using (html.beginform("create", "listing", formmethod.post, new { enctype = "multipart/form-data" }))
and;
<input type="file" name="file" id="file" /> <input type="submit" value="submit" />
this works me, however, i've not tested see how performs when attempting view each individual listing id, might not correct solution me.
Comments
Post a Comment