1
Answer

how to pass image path to server side using ajax mvc?

Ivin Raj S

Ivin Raj S

7y
213
1
when i click submit button am getting everything including image path data in ajax.but in server side am not getting image path data renaming value am getting..
 
@using (Html.BeginForm("AddDoctor", "Doctor", FormMethod.Post, new { id = "myForm", enctype = "multipart/form-data"  }))         {             @Html.AntiForgeryToken()             @Html.ValidationSummary(true)             <section class="tabs-section">                 <div class="tabs-section-nav tabs-section-nav-icons">                     <div class="tbl">                         <ul class="nav" role="tablist" id="myLinks">                             <li class="nav-item">                                 <a class="nav-link active" href="#tabs-1-tab-1" role="tab" data-toggle="tab">                                     <span class="nav-link-in">                                         <i class="fa fa-user-md"></i>                                         Doctor Registration                                     </span>                                 </a>                             </li>                             <li class="nav-item">                                 <a class="nav-link" href="#tabs-1-tab-2" role="tab" data-toggle="tab">                                     <span class="nav-link-in">                                         <span class="fa fa-product-hunt"></span>                                         Doctor Experience                                     </span>                                 </a>                             </li>                             <li class="nav-item">                                 <a class="nav-link" href="#tabs-1-tab-3" role="tab" data-toggle="tab">                                     <span class="nav-link-in">                                         <i class="fa fa-building"></i>                                         Doctor Education                                     </span>                                 </a>                             </li>                             <li class="nav-item">                                 <a class="nav-link" href="#tabs-1-tab-4" role="tab" data-toggle="tab">                                     <span class="nav-link-in">                                         <i class="fa fa-stethoscope"></i>                                         Doctor Clinic Information                                     </span>                                 </a>                             </li>                         </ul>                     </div>                 </div><!--.tabs-section-nav-->                 <div class="tab-content" id="myTabs">                     <div role="tabpanel" class="tab-pane fade in active show" id="tabs-1-tab-1">                         <br />                         <br />                          <form id="form" action="">                             <section>                                 <div id="company">                                     <div class="row">                                         <div class="col-lg-4">                                             <fieldset class="form-group">                                                 <label class="form-label semibold control-label">Tittle</label>                                                 <select class="select2-arrow" id="txtTittle" name="StudentGender">                                                     <option>-- Select Tite --</option>                                                     <option value="Dr.">Dr.</option>                                                     <option value="Mr.">Mr.</option>                                                     <option value="Mrs.">Mrs.</option>                                                     <option value="Miss.">Miss.</option>                                                     <option value="Ms.">Ms.</option>                                                     <option value="Prof.">Prof.</option>                                                 </select>                                                 @Html.ValidationMessageFor(model => Model.DoctorRegistration.Tittle, null, new { @style = "color: red" })                                             </fieldset>                                         </div>                                         <div class="col-lg-4">                                             <fieldset class="form-group">                                                 <label class="form-label semibold control-label">Doctor Name</label>                                                 @Html.TextBoxFor(model => Model.DoctorRegistration.DoctorName, new { @class = "form-control", @id = "txtDoctorName", placeholder = "Enter the Doctor Name" })                                                 @Html.ValidationMessageFor(model => Model.DoctorRegistration.DoctorName, null, new { @style = "color: red" })                                             </fieldset>                                         </div>                                         <div class="col-lg-4">                                             <fieldset class="form-group">                                                 <label class="form-label semibold control-label">Doctor Code</label>                                                 @Html.TextBoxFor(model => Model.DoctorRegistration.DoctorCode, new { @class = "form-control", @id = "txtDoctorCode", placeholder = "Enter the Doctor Code" })                                             </fieldset>                                         </div>                                     </div><!--.row-->                                     <br />                                     <div class="row">                                         <div class="col-lg-4">                                             <fieldset class="form-group">                                                 <label class="form-label semibold control-label">Registration Number</label>                                                 @Html.TextBoxFor(model => Model.DoctorRegistration.RegistrationNo, new { @class = "form-control", @id = "txtRegistrationNo", placeholder = "Doctor Registration Number" })                                             </fieldset>                                         </div>                                         <div class="col-lg-4">                                             <fieldset class="form-group">                                                 <label class="form-label semibold control-label">Gender</label>                                                 <br />                                                 <div class="form-group form-group-radios">                                                     <div class="radio">                                                         <input type="radio" name="optionsRadios" id="radio-1" value="M">                                                         <label for="radio-1">Male </label>                                                         &nbsp; &nbsp;&nbsp; &nbsp;<input type="radio" name="optionsRadios" id="radio-2" value="F">                                                         <label for="radio-2">Female</label>                                                     </div>                                                 </div>                         </fieldset>                                         @Html.ValidationMessageFor(model => Model.DoctorRegistration.Gender, null, new { @style = "color: red" })                                     </div>                                     <div class="col-lg-4">                                         <fieldset class="form-group">                                             <label class="form-label semibold control-label">Date Of Birth</label>                                             @*@Html.TextBoxFor(model => Model.DoctorRegistration.DocDOB, new { @class = "form-control", @id = "txtDocDOB", placeholder = "Enter the Doctor Code" })*@                                             <div class='input-group date'>                                                 <input id="txtDocDOB" type="text" value="24/10/1984" class="form-control">                                                 <span class="input-group-addon">                                                     <i class="font-icon font-icon-calend"></i>                                                 </span>                                             </div>                                         </fieldset>                                         @Html.ValidationMessageFor(model => Model.DoctorRegistration.DocDOB, null, new { @style = "color: red" })                                     </div>                                 </div><!--.row-->                                 <br />                                 <div class="row">                                     <div class="col-lg-4">                                         <fieldset class="form-group">                                             <label class="form-label semibold control-label">Address Line 1</label>                                             @Html.TextBoxFor(model => Model.DoctorRegistration.DocAddress1, new { @class = "form-control", @id = "txtDocAddress1", placeholder = "Enter the Address Line 1" })                                             @Html.ValidationMessageFor(model => Model.DoctorRegistration.DocAddress1, null, new { @style = "color: red" })                                         </fieldset>                                     </div>                                     <div class="col-lg-4">                                         <fieldset class="form-group">                                             <label class="form-label semibold control-label">Address Line 2</label>                                             @Html.TextBoxFor(model => Model.DoctorRegistration.DocAddress2, new { @class = "form-control", @id = "txtDocAddress2", placeholder = "Enter the Address Line 2" })                                         </fieldset>                                     </div>                                     <div class="col-lg-4">                                         <fieldset class="form-group">                                             <label class="form-label semibold control-label">Country</label>                                             @Html.DropDownList("Country", null, "--- Select Country ---", new { @class = "select2-arrow" })                                             @Html.ValidationMessageFor(model => Model.DoctorRegistration.Country, null, new { @style = "color: red" })                                         </fieldset>                                     </div>                                 </div><!--.row-->                                 <br />                                 <div class="row">                                     <div class="col-lg-4">                                         <fieldset class="form-group">                                             <label class="form-label semibold control-label">State</label>                                             <select id="state" class="select2-arrow"></select>                                             @Html.ValidationMessageFor(model => Model.DoctorRegistration.State, null, new { @style = "color: red" })                                             @Html.ValidationMessageFor(model => Model.DoctorRegistration.State, null, new { @style = "color: red" })                                         </fieldset>                                     </div>                                     <div class="col-lg-4">                                         <fieldset class="form-group">                                             <label class="form-label semibold control-label">City</label>                                             @Html.TextBoxFor(model => Model.DoctorRegistration.City, new { @class = "form-control", @id = "txtCity", placeholder = "Enter the City Name" })                                             @Html.ValidationMessageFor(model => Model.DoctorRegistration.City, null, new { @style = "color: red" })                                         </fieldset>                                     </div>                                     <div class="col-lg-4">                                         <fieldset class="form-group">                                             <label class="form-label semibold control-label">Pincode</label>                                             @Html.TextBoxFor(model => Model.DoctorRegistration.Pincode, new { @class = "form-control", @id = "txtPincode", placeholder = "Enter the Pincode" })                                             @Html.ValidationMessageFor(model => Model.DoctorRegistration.Pincode, null, new { @style = "color: red" })                                         </fieldset>                                     </div>                                 </div><!--.row-->                                 <br />                                 <div class="row">                                     <div class="col-lg-4">                                         <fieldset class="form-group">                                             <label class="form-label semibold control-label">Country Code</label>                                             @Html.DropDownList("CountryCode", null, "--- Select Country Code ---", new { @class = "select2-arrow" })                                             @Html.ValidationMessageFor(model => Model.DoctorRegistration.CountryCode, null, new { @style = "color: red" })                                         </fieldset>                                     </div>                                     <div class="col-lg-4">                                         <fieldset class="form-group">                                             <label class="form-label semibold control-label">Mobile Number</label>                                             @Html.TextBoxFor(model => Model.DoctorRegistration.Mobile, new { @class = "form-control", @id = "txtMobile", placeholder = "Enter the Mobile Number" })                                             @Html.ValidationMessageFor(model => Model.DoctorRegistration.Mobile, null, new { @style = "color: red" })                                         </fieldset>                                     </div>                                     <div class="col-lg-4">                                         <fieldset class="form-group">                                             <label class="form-label semibold control-label">Phone</label>                                             @Html.TextBoxFor(model => Model.DoctorRegistration.Phone, new { @class = "form-control", @id = "txtPhone", placeholder = "Enter the Phone Number" })                                         </fieldset>                                     </div>                                 </div><!--.row-->                                 <br />                                 <div class="row">                                     <div class="col-lg-4">                                         <fieldset class="form-group">                                             <label class="form-label semibold control-label">Email</label>                                             @Html.TextBoxFor(model => Model.DoctorRegistration.Email, new { @class = "form-control", @id = "txtEmail", placeholder = "Enter the Email" })                                             @Html.ValidationMessageFor(model => Model.DoctorRegistration.Email, null, new { @style = "color: red" })                                         </fieldset>                                     </div>                                     <div class="col-lg-4">                                         <fieldset class="form-group">                                             <label class="form-label semibold control-label">Website</label>                                             @Html.TextBoxFor(model => Model.DoctorRegistration.Website, new { @class = "form-control", @id = "txtWebsite", placeholder = "Enter the Website" })                                         </fieldset>                                     </div>                                     <div class="col-lg-4">                                         <fieldset class="form-group">                                             <label class="form-label semibold control-label">Aadhaar Card Number</label>                                             @Html.TextBoxFor(model => Model.DoctorRegistration.AadhaarCardNo, new { @class = "form-control", @id = "txtAadhaarCardNo", placeholder = "Enter the AadhaarCard NUmber" })                                         </fieldset>                                     </div>                                 </div><!--.row-->                                 <br />                                 <div class="row">                                     <div class="col-lg-4">                                         <fieldset class="form-group">                                             <label class="form-label semibold control-label">Language</label>                                             <div class="col-lg-12">                                                 <textarea id="txtLanguage">English,Tamil</textarea>                                             </div>                                         </fieldset>                                     </div>                                     <div class="col-lg-4">                                         <fieldset class="form-group">                                             <label class="form-label semibold control-label">AboutDoctor</label>                                             @Html.TextAreaFor(model => Model.DoctorRegistration.AboutDoctor, new { @class = "form-control maxlength-simple", @id = "txtAboutDoctor", maxlength = "500", placeholder = "Max length 500 chars" })                                         </fieldset>                                     </div>                                     <div class="col-lg-4">                                         <fieldset class="form-group">                                             <label class="form-label semibold control-label">Status</label>                                             <select class="select2-arrow valid" id="Status" name="Status">                                                 <option>---Select Status---</option>                                                 <option value="Y">Active</option>                                                 <option value="N">InActive</option>                                             </select>                                         </fieldset>                                     </div>                                 </div><!--.row-->                                 <br />                                 <div class="row">                                     <div class="col-lg-4">                                         <fieldset class="form-group">                                             <label class="form-label semibold">Upload Company Logo</label>&nbsp;&nbsp;                                             <span class="btn btn-rounded btn-file">                                                 <span>Choose file</span>                                                 <input type="file" name="postedFile" id="txtUploadImage">                                             </span>                                         </fieldset>                                     </div>                                     <div class="col-lg-4">                                         <div class="col-lg-4">                                             <fieldset class="form-group">                                                 <label class="form-label semibold">Perview Image</label>&nbsp;&nbsp;                                                 <img id="image_upload_preview" src="http://placehold.it/100x100" alt="your image" />                                                 <a id="remove" onclick="javascript:ClearFileUploadControl();" style="display: none; cursor: pointer;">Remove</a>                                             </fieldset>                                         </div>                                     </div>                                 </div><!--.row-->                                 <br />                                 <br />                                 <input type="submit" name="Submit" id="SaveDoctor" value="Save" class="btn btn-rounded btn-inline btn-success" />                                 <span style="color:green">@ViewBag.Message</span>                             </div>                         </section>                     </form>                  </div>

Ajax method:

function GetInfo() { var gender = $("input[name='optionsRadios']:checked").val(); var postedFile = $("#txtUploadImage").val(); var Doctor = { Tittle: $("#txtTittle").val(), DoctorName: $("#txtDoctorName").val(), DoctorCode: $("#txtDoctorCode").val(), RegistrationNo: $("#txtRegistrationNo").val(), Gender: gender, DocDOB: $("#txtDocDOB").val(), DocAddress1: $("#txtDocAddress1").val(), DocAddress2: $("#txtDocAddress2").val(), Country: $("#Country").val(), State: $("#state").val(), City: $("#txtCity").val(), Pincode: $("#txtPincode").val(), CountryCode: $("#CountryCode").val(), Mobile: $("#txtMobile").val(), Phone: $("#txtPhone").val(), Email: $("#txtEmail").val(), Website: $("#txtWebsite").val(), AadhaarCardNo: $("#txtAadhaarCardNo").val(), Language: $("#txtLanguage").val(), AboutDoctor: $("#txtAboutDoctor").val(), Status: $("#Status").val() }; var mainModel = {};             mainModel.DoctorRegistration = Doctor;              $.ajax({                 type: "POST",                 url: "/Doctor/AddDoctor",                 data: JSON.stringify(mainModel, postedFile),                 contentType: "application/json; charset=utf-8",                 dataType: "json",                 success: OnSuccess,                 failure: function (response) {                     alert(response.d); } }); }         function OnSuccess(response) { //alert(response.id); if (response.id > 0) { var linkHref = "tabs-1-tab-1"; } }

Controller:

[HttpPost] public ActionResult AddDoctor(DoctorMainModel mainModel, HttpPostedFileBase postedFile) { try { Country_Bind(); CountryCode_Bind(); if (ModelState.IsValid) { DoctorReg obj = new DoctorReg(); int id = obj.AddDoctorRegistration(mainModel); if (id != 0) { Session["docID"] = id.ToString(); AddDoctorImage(postedFile); ViewBag.Message = "Company added successfully"; return Json(new { id = Session["docID"] }); } } return View(); } catch { return View(); } } private void AddDoctorImage(HttpPostedFileBase postedFile) { int docId = Convert.ToInt32(Session["docID"]); if (postedFile != null) { string path = Server.MapPath("~/Doctor/"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var filename = docId.ToString() + ".png";                 postedFile.SaveAs(path + filename); ViewBag.Message = "File uploaded successfully."; } }

First Image

Second image

All value pass to the controller but postedFile value am not getting

Value pass

null value

 
Answers (1)