سلام
من با کمک این مقاله
http://barnamenevisan.org/Articles/Article3559.html
می خوام فرم ثبت نام پیش فرض asp.net mvc رو در یک مودال به کاربر نمایش بدم، که البته نمایش داده میشه و ثبت کاربر هم انجام میشه و کلاینت ساید ولیدیشن ها هم انجام میشه، یعنی required ها، یا فرمت اشتباه ایمیل.
ولی مشکلم اینجاست که server side ولیدیشن ها در مودال به کاربر نمایش داده نمیشه، مثلاً تکراری بودن ایمیل یا اشتباه بودن پسورد یا نام کاربری.
در صورتی که همین ولیدیشن بدون مودال و بدون ajax.beginform به صورت زیر در validation summery نمایش داده میشه.
Email 'test@yahoo.com' is already taken
ممنون می شم راهنمایی کنید.
البته ولیدیشن صورت می گیره چون table رو که چک می کنم در صورت تکراری بودن ایمیل درجی اتفاق نمی افته ولی خوب مسئله اینه که به کاربر هم پیغامی در مودال نمایش داده نمیشه که متوجه اشتباهش بشه.
کد Action و View را قرار دهید
به احتمال زیاد در Return اکشن مربوطه مشکلی وجود دارد
سلام، عذر می خوام که دیر پاسخ دادم
این کد اکشن
// POST: /AccountModal/Register [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { //خودم کامنت کردم //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); //http://www.asp.net/identity/overview/features-api/account-confirmation-and-password-recovery-with-aspnet-identity var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Action( "ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>"); // ViewBag.Link = callbackUrl; // Used only for initial demo. //return PartialView("_DisplayEmail"); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); //خودم کامنت کردم //return RedirectToAction("Index", "Home"); } AddErrors(result); } // If we got this far, something failed, redisplay form return PartialView(model); }
و این هم کدهای view
@model tablighkadeui.Models.RegisterViewModel @{ ViewBag.Title = "ایجاد حساب کاربری"; } @*http://stackoverflow.com/questions/8927933/unobtrusive-validation-doesnt-work-with-ajax-beginform*@ <script type="text/javascript" language=javascript> $.validator.unobtrusive.parse(document); </script> <script type="text/javascript"> function onBegin() { $("#divLoading").html('<image src="/ajax-loader.gif" alt="Loading, please wait" />'); } function onComplete() { $("#divLoading").html(""); } </script> <h2>Create</h2> <div id="divLoading"></div> @*@using (Ajax.BeginForm("Register", "Account", FormMethod.Post)) {*@ @using (Ajax.BeginForm("Register", "AccountModal", FormMethod.Post, new AjaxOptions() { OnSuccess = "successRegister", OnBegin = "onBegin", OnComplete = "onComplete" }, new { @class = "registration-form" })) { @Html.AntiForgeryToken() <h4>@ViewBag.Title</h4> <hr /> @Html.ValidationSummary("", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.TextBoxFor(m => m.Email, new { @class = "form-control" }) </div> </div> <div class="form-group"> @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.PasswordFor(m => m.Password, new { @class = "form-control" }) </div> </div> <div class="form-group"> @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" }) </div> </div> <button type="submit" class="btn">ثبت نام</button> } @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
کدهای مودال هم در layout.cshtml قرار داره
<!-- MODAL --> <div class="modal modal-success fade" id="modal-register-success" tabindex="-1" role="dialog" aria-labelledby="modal-register-label" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal"> <span aria-hidden="true">×</span><span class="sr-only">Close</span> </button> <h3 class="modal-title" id="modal-register-label">Sign up now</h3> <p>Fill in the form below to get instant access:</p> </div> <div class="modal-body" id="modal-body"> <p> Please check your email and confirm your email address. </p> </div> </div> </div> </div>
این هم کدهای جی کوئری که در layout.cshtml قرار داره
<script> function Register() { $.ajax({ url: "/AccountModal/Register", type: "Get", }).done(function (result) { $('#modal-register').modal('show'); $('#modal-register-label').html('افزودن شخص جدید'); $('#modal-body').html(result); }); } function successRegister() { //$('#modal-register').modal('hide'); //$('#modal-register-success').modal('show'); } function Login() { $.ajax({ url: "/AccountModal/Login", type: "Get", }).done(function (result) { $('#modal-login').modal('show'); $('#modal-login-label').html('ورود'); $('#modal-login-body').html(result); }); } //http://stackoverflow.com/questions/9583503/asp-net-mvc-partial-view-redirect-or-show-error function successLogin(result) { $('#modal-login').modal('hide'); var returnUrl = result.returnUrl; if (returnUrl) { window.location.href = returnUrl; } else { // TODO: update the target form element with the returned partial html } } $(document).ready(function () { $("#RegAd").click(function () { Login(); }); }); </script> <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script> <script src="~/Scripts/jquery.validate.min.js"></script> <script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
ممنون
استاد ممنون، الان این لینک رو خوندم
http://stackoverflow.com/questions/12038544/mvc4-ajax-forms-how-to-display-server-side-validation
متوجه شدم باید از UpdateTargetID استفاده می کردم، تست کردم حل شد.
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)