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)
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."; } }


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


1 Reply
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.

Raja Mouli AnkireddyPosted Oct 24, 2017, 12:45 PM