c# 4.0 - Uploading and retrieving image and video url in ASP.NET MVC4 Internet Application -
i have following model in mvc 4 internet application
public class eventmodel { public int eventid { get; set;} public string eventtitle { get; set;} public string imageurl { get; set;} public string videourl { get;set} }
here's create.cshtml code image , video
@using(html.beginform()) { @html.antiforgerytoken() @html.validationsummary(true) <div> @html.labelfor(model =>model.imageurl) </div> <div> @html.editorfor(model =>model.imageurl) @html.validationmessagefor(model =>model.imageurl) </div> <div> @html.labelfor(model =>model.videourl) </div> <div> @html.editorfor(model =>model.videourl) @html.validationmessagefor(model =>video.imageurl) </div> }
this controller code post method on create
[httppost] [validateantiforgerytoken] public actionresult create(eventmodel eventmodel) { if (modelstate.isvalid) { _db.eventmodels.add(eventmodel); _db.savechanges(); return redirecttoaction("index"); } return view(eventmodel); }
how can make create page display buttons upload image users computer. how modify controller action store paths on database , how retrieve image on index.cshtml page.
for file uploading try this:
in controller:
[httppost] public actionresult create(eventmodel eventmodel, httppostedfilebase file) { if (modelstate.isvalid) { var filename = path.getfilename(file.filename); var path = path.combine(server.mappath("~/uploads/photo/"), filename); file.saveas(path); tyre.url = filename; _db.eventmodels.addobject(eventmodel); _db.savechanges(); return redirecttoaction("index"); } return view(eventmodel); }
and view:
<div> image <input type="file" name="file" id="file" /> @html.hiddenfor( model => model.imageurl) @html.validationmessagefor( model => model.url ) </div>
Comments
Post a Comment