asp.net - Server Error in Application - The resource cannot be found when trying to add an image to database -
im getting error after adding line.
@using (html.beginform( "courseregister", "courseplancontroller", formmethod.post, new { enctype = "multipart/form-data" })) this controller (courseplancontroller) , create action.
[httppost] [allowanonymous] [validateantiforgerytoken] public actionresult courseregister(coursecategory model, httppostedfilebase upload) { try { if (modelstate.isvalid) { if (upload != null && upload.contentlength > 0) { var avatar = new file { filename = system.io.path.getfilename(upload.filename), filetype = filetype.avatar, contenttype = upload.contenttype }; using (var reader = new system.io.binaryreader(upload.inputstream)) { avatar.content = reader.readbytes(upload.contentlength); } model.files = new list<file> { avatar }; } try { coursecategory ca = new coursecategory() { coursefield = model.coursefield, coursesubject = model.coursesubject, coursename = model.coursename, startdate = model.startdate, price = model.price, courselevel = model.courselevel, duration = model.duration, qualifications = model.qualifications, overview = model.overview, description = model.description, institute = model.institute, contact = model.contact, email = model.email,files = model.files }; this._coursemanagersvcinstance.createnewcourse(ca); viewbag.successmessage = "<p>success!</p>"; return redirecttoaction("index"); } catch (membershipcreateuserexception e) { //modelstate.addmodelerror("", errorcodetostring(e.statuscode)); } } } catch (retrylimitexceededexception /* dex */) { modelstate.addmodelerror("", "unable save changes. try again, , if problem persists see system administrator."); } return view(); } this view. (courseregister.cshtml)
@model smartjob.domain.models.coursecategory @{ viewbag.title = "courseregister"; } @using (html.beginform("courseregister", "coursecategory", formmethod.post, new { enctype = "multipart/form-data" })) { @html.antiforgerytoken() @html.validationsummary(true) <fieldset> @*<legend>course category</legend>*@ <div class="editor-label"> @html.labelfor(model => model.coursefield) </div> <div class="editor-field"> @*@html.editorfor(model => model.coursefield) @html.validationmessagefor(model => model.coursefield)*@ @html.dropdownlistfor(model => model.coursefield, new selectlist(smartjob.web.ui.helper.professionalcoursedropdownhelper.coursecategoriesdropdown, "value", "value", 0), "", new { @class = "textboxfor" }) @html.validationmessagefor(model => model.coursefield, null, new { @class = "errors" }) </div> <div class="editor-label"> @html.labelfor(model => model.coursesubject) </div> <div class="editor-field"> @html.editorfor(model => model.coursesubject) @html.validationmessagefor(model => model.coursesubject, null, new { @class = "errors" }) </div> <div class="editor-label"> @html.labelfor(model => model.coursename) </div> <div class="editor-field"> @html.editorfor(model => model.coursename) @html.validationmessagefor(model => model.coursename, null, new { @class = "errors" }) </div> <div class="editor-label"> @html.labelfor(model => model.startdate) </div> <div class="editor-field"> @html.editorfor(model => model.startdate) @html.validationmessagefor(model => model.startdate, null, new { @class = "errors" }) </div> <div class="editor-label"> @html.labelfor(model => model.price) </div> <div class="editor-field"> @html.editorfor(model => model.price) @html.validationmessagefor(model => model.price, null, new { @class = "errors" }) </div> <div class="editor-label"> @html.labelfor(model => model.courselevel) </div> <div class="editor-field"> @*@html.editorfor(model => model.coursetype) @html.validationmessagefor(model => model.coursetype, null, new { @class = "errors" })*@ @html.dropdownlistfor(model => model.courselevel, new selectlist(smartjob.web.ui.helper.professionalcoursedropdownhelper.courselevelsdropdrown, "value", "value", 0), "", new { @class = "textboxfor" }) @html.validationmessagefor(model => model.courselevel, null, new { @class = "errors" }) </div> <div class="editor-label"> @html.labelfor(model => model.duration) </div> <div class="editor-field"> @html.editorfor(model => model.duration) @html.validationmessagefor(model => model.duration, null, new { @class = "errors" }) </div> <div class="editor-label"> @html.labelfor(model => model.qualifications) </div> <div class="editor-field"> @html.editorfor(model => model.qualifications) @html.validationmessagefor(model => model.qualifications, null, new { @class = "errors" }) </div> <div class="editor-label"> @html.labelfor(model => model.overview) </div> <div class="editor-field"> @html.editorfor(model => model.overview) @html.validationmessagefor(model => model.overview, null, new { @class = "errors" }) </div> <div class="editor-label"> @html.labelfor(model => model.description) </div> <div class="editor-field"> @html.editorfor(model => model.description) @html.validationmessagefor(model => model.description, null, new { @class = "errors" }) </div> <div class="editor-label"> @html.labelfor(model => model.institute) </div> <div class="editor-field"> @html.editorfor(model => model.institute) @html.validationmessagefor(model => model.institute, null, new { @class = "errors" }) </div> <div class="editor-label"> @html.labelfor(model => model.contact) </div> <div class="editor-field"> @html.editorfor(model => model.contact) @html.validationmessagefor(model => model.contact, null, new { @class = "errors" }) </div> <div class="editor-label"> @html.labelfor(model => model.email) </div> <div class="editor-field"> @html.editorfor(model => model.email) @html.validationmessagefor(model => model.email, null, new { @class = "errors" }) </div> @*------------upload image------------------*@ <div class="editor-label"> @html.labelfor(model => model.files) </div> <div class="editor-field"> <input type="file" id="avatar" name="upload" /> </div> @*------------------------------------------*@ <p> <p></p> <input class="btn btn-default" type="submit" value="create" /> </p> </fieldset> } this model class (coursecategory.cs)
public class coursecategory { [databasegenerated(databasegeneratedoption.identity)] public int id { get; set; } [required(errormessage = "please select field of study")] [display(name = "field of study")] public string coursefield { get; set; } [required(errormessage = "please select course subject")] [display(name = "course subject")] public string coursesubject { get; set; } [required(errormessage = "please select course name")] [display(name = "course name", prompt = "course name")] public string coursename { get; set; } [datatype(datatype.date)] [displayformat(dataformatstring = "{0:yyyy-mm-dd}", applyformatineditmode = true)] [display(name = "start date")] public datetime startdate { get; set; } [display(name = "price (rs.)")] public double price { get; set; } [required(errormessage = "please select course type")] [display(name = "course/program level")] public string courselevel { get; set; } [display(name = "duration")] public string duration { get; set; } [display(name = "qualifications")] public string qualifications { get; set; } [display(name = "overview")] [datatype(datatype.multilinetext)] public string overview { get; set; } [display(name = "description")] [datatype(datatype.multilinetext)] public string description { get; set; } [display(name = "institute")] public string institute { get; set; } [display(name = "contact details" )] [regularexpression(@"^\d{10}$", errormessage="please provide valid contact number (ex : 0123456789) ")] public string contact { get; set; } [display(name = "contact e-mail")] [regularexpression(@"^([0-9a-za-z]([\+\-_\.][0-9a-za-z]+)*)+@(([0-9a-za-z][-\w]*[0-9a-za-z]*\.)+[a-za-z0-9]{2,3})$", errormessage = "please provide valid email address")] public string email { get; set; } [display(name = "attachments")] public virtual icollection<file> files { get; set; } }
you getting 404 error because giving beginform method incorrect controller name. in land of mvc, controller class such mycontroller used in urls controller suffix dropped. need change line:
@using (html.beginform( "courseregister", "courseplan", formmethod.post, new { enctype = "multipart/form-data" }))
Comments
Post a Comment