سلام
من میخواهم با استفاده از Ajax.BeginForm عکس را آپلود کنم ولی وقتی در اکشن دریافت میکنم مقدار آن null هست.
ولی اگه بیام فرم را تبدیل Html.BeginForm کنم به درستی کار می کنه و عکس رو برا اکشن میفرسته ولی اگه با Ajax.BeginForm برم کار نمی کنه .
این فرم به درستی کار می کنه
@using (Html.BeginForm("Create", "LettersOperations", FormMethod.Post, new { @class = "form-horizontal", role = "form", enctype = "multipart/form-data" })) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>LettersOperation</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.Text, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Text, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Text, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Image, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> <input type="file" name="File1" /> </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> }
ولی با ایجکس فرم کار نمی کنه
@using (Ajax.BeginForm("Create", "LettersOperations", new AjaxOptions { UpdateTargetId = "ajaxshow1", LoadingElementId = "gifloading", InsertionMode = InsertionMode.Replace, OnSuccess = "OnSuccess(data)" }, new { enctype = "multipart/form-data" })) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>LettersOperation</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.Text, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Text, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Text, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Image, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> <input type="file" name="File1" /> </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> }
public JsonResult Create(LettersOperation lettersOperation,HttpPostedFileBase File1) { if (ModelState.IsValid) { if (File1 != null && File1.ContentLength > 0) { lettersOperation.Image= new byte[File1.ContentLength]; File1.InputStream.Read(lettersOperation.Image,0, File1.ContentLength); string ImageName = System.IO.Path.GetFileName(File1.FileName); string physicalPath = Server.MapPath("~/img/" + ImageName); File1.SaveAs(physicalPath); lettersOperation.ImagePath = "img/" + ImageName; db.Letteroperations.Add(lettersOperation); db.SaveChanges(); return Json(new { message = "اطلاعات نیرو با موفقیت ثبت شد", status = true, t = "ثبت شد" }, JsonRequestBehavior.AllowGet); } } return Json(lettersOperation); }
با استفاده از ajax.beginform نمی توانید این کار را انجام دهید. بهتر است توسط Jquery این کار را انجام دهید.
لینک زیر را مشاهده کنید: jquery - Ajax.BeginForm in MVC to upload files - Stack Overflow
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)