0
ساختن صفحه ثبت نام در ASP MVC با Ajax
درود . دوستان من یه صفحه ثبت نام ساختم ک در این صفحه من عکس کاربر رو هم باید دریافت کنم . ولی کلا هیچ عکس العملی نشون نمیده کدهای ajax من و وقتی رو ثبت نام کلیلک میکنم یه صقحه خالی برام میاد . باید چیکار کنم ؟ مشکل کدهای من کجاس ؟ دوستان اگر کسی بلده لطفا راهنمایی کنه . میدونم وقت گیر و حوصله بره ولی خب عوضش به یه نفر کمک میکنید . ممنون
<div class="Edit_Add_Student"> @using (Html.BeginForm("EditStudent", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.AntiForgeryToken(); <div class="content"> <div class="Content_Add_Edit" style="float: right;"> <div class="form-group"> @Html.LabelFor(x => x.StudentName) @Html.TextBoxFor(x => x.StudentName, htmlAttributes: new { @class = "form-control", @id = "StudentName" }) </div> <div class="form-group"> @Html.LabelFor(x => x.StudentFamily) @Html.TextBoxFor(x => x.StudentFamily, htmlAttributes: new { @class = "form-control", @id = "StudentFamily" }) </div> <div class="form-group"> @Html.LabelFor(x => x.StudentBirthDay) @Html.TextBoxFor(x => x.StudentBirthDay, htmlAttributes: new { @class = "form-control", id = "StudentBirthDay" }) </div> <div class="form-group"> @Html.LabelFor(x => x.StudentShensnameNo) @Html.TextBoxFor(x => x.StudentShensnameNo, htmlAttributes: new { @class = "form-control", @id = "StudentShensnameNo" }) </div> <div class="form-group"> @Html.LabelFor(x => x.Age) @Html.TextBoxFor(x => x.Age, htmlAttributes: new { @class = "form-control", @id = "Age" }) </div> <div class="form-group"> @Html.LabelFor(x => x.Email) @Html.TextBoxFor(x => x.Email, htmlAttributes: new { @class = "form-control", @id = "Email" }) </div> </div> <div class="Content_Add_Edit" style="float: right; margin-right: 20px;"> <div class="form-group"> @Html.LabelFor(x => x.Reshte) @Html.DropDownList("Reshte", null, htmlAttributes: new { @class = "form-control", @id = "Reshte" }) </div> <div class="form-group"> @Html.LabelFor(x => x.Paye) @Html.DropDownList("Paye", null, htmlAttributes: new { @class = "form-control", @id = "Paye" }) </div> <div class="form-group"> @Html.LabelFor(x => x.StudentAddress) @Html.TextAreaFor(x => x.StudentAddress, htmlAttributes: new { @class = "form-control", @id = "Paye" }) </div> <div class="form-group"> @Html.LabelFor(x => x.StudentHomeTel) @Html.TextBoxFor(x => x.StudentHomeTel, htmlAttributes: new { @class = "form-control", @id = "StudentHomeTel" }) </div> <div class="form-group"> @Html.LabelFor(x => x.StudentPhone) @Html.TextBoxFor(x => x.StudentPhone, htmlAttributes: new { @class = "form-control", @id = "StudentPhone" }) </div> </div> <div class="Content_Add_Edit" style="float: right; margin-right: 20px;"> <div class="form-group"> @Html.LabelFor(x => x.StudentImage) <input type="file" class="form-control" name="fileBase" id="fileBase" /> </div> <div class="form-group"> @Html.LabelFor(x => x.UserName) @Html.TextBoxFor(x => x.UserName, htmlAttributes: new { @class = "form-control", @id = "UserName" }) </div> <div class="form-group"> @Html.LabelFor(x => x.Password) @Html.TextBoxFor(x => x.Password, htmlAttributes: new { @class = "form-control", @id = "Password" }) </div> </div> <button type="submit" class="btn btn-success" id="AddStudent" style="margin-top: 12px; margin-right: 20px; width: 303px; height: 39px;"> ثبت دانش آموز </button> @*<button type="reset" class="btn btn-danger" style="margin-top: 24px; margin-right: 0;width: 120px;"> انصراف</button>*@ </div> } </div> <script> function submitForm(form) { // form= تگ فرم که حاوی تگهای اینپوت است var fd = new FormData(); fd.append("fileBase", fileBase.files[0]); $.ajax({ url: form.action + "?" + $("#AddStudent").serialize(), type: "POST", data: fd, enctype: 'multipart/form-data', processData: false, contentType: false }).done(function (data) { alert('Your Message'); }); } </script>
.
Controller : [HttpPost] [ValidateAntiForgeryToken] public async Task<JsonResult> EditStudent(int? id, StudentRegister model, HttpPostedFileBase fileBase) { ViewBag.Reshte = new SelectList(_db.Tbl_Reshte, "ReshteID", "ReshteName"); ViewBag.Paye = new SelectList(_db.Tbl_Paye, "PayeID", "PayeName"); if (id == null) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.UserName, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); model.UserID = user.Id; if (result.Succeeded) { await UserManager.AddToRoleAsync(userId: user.Id, role: "student"); var file = Request.Files[0]; var ext = System.IO.Path.GetExtension(file.FileName); if (ext == ".jpg" || ext == ".png" || ext == ".jpeg") { string filename = model.StudentName + " " + model.StudentFamily + "-" + model.StudentShensnameNo + ext; file.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Image/StudentImage/" + filename))); model.StudentImage = filename; } Student.InsertStudent(model); ReshtePaye.InsertReshte_Paye(model); } } } // If we got this far, something failed, redisplay form return new JsonResult() { Data = new { } }; }
2 پاسخ
0
سلام دوست عزیز، بالای اکشن EditStudent به صورت زیر HttpPost رو قرار بدید:
[HttpPost] [ValidateAntiForgeryToken] public async Task<JsonResult> EditStudent(....
0
قرار دادم که