Note that there are some explanatory texts on larger screens.

plurals
  1. POmvc3 - ajax form submit and server side validation
    primarykey
    data
    text
    <p>I am sure this has been asked already, but I have been trying all the solutions given, but none of the given solution is solving my problem. Probably, I am not implementing it right.</p> <p>I am using MVC3, razor, jQuery.</p> <p>I have a new user registration form. Once registration is successful, I want to display the successful message (different on type of registration) in a dialog box.</p> <p>If there is any error with ModelState, means ModelState.IsValid == false, I want to display errors in ValidationSummary.</p> <p>View </p> <pre><code>function OnBegin () { alert('begin'); return $('form').validate().form(); } function OnSuccess () { alert('success'); DisplaySucess()} function OnFailure () { alert('failure'); } function DisplaySuccess() { var typeOfRegistration = $('input:radio[name=RegistrationType]:checked').val(); var msg; if (typeOfRegistration == 0) { msg = "You are successfully enrolled for Algebra classes."; } if (typeOfRegistration == 1) { msg = "You are registered with your email address. Someone would contact you with an email."; } if (typeOfRegistration == 2) { msg = "Thank you for filling up this form. One welcome packet would be delivered with in next 15 days"; } msg = msg.replace(/\n/g, '&lt;br /&gt;'); $('#dialog').html(msg); $('#dialog').dialog('open'); } </code></pre> <p></p> <pre><code>@model Models.RegistrationModel @using (Ajax.BeginForm("NewRegistration", "Account", new AjaxOptions() { OnBegin="OnBegin", OnSuccess = "OnSuccess", OnFailure = "OnFailure" })) { @Html.ValidationSummary() &lt;div&gt; Register&lt;br /&gt; @Html.RadioButton("RegistrationType ", "0", true) New Algebra class&lt;br /&gt; @Html.RadioButton("RegistrationType ", "1") New user registration by email&lt;br /&gt; @Html.RadioButton("RegistrationType ", "2") New user registration by mailing address&lt;br/&gt; ..some more types &lt;/div&gt; &lt;div&gt; …here I display/hide controls based on Registration type selected. &lt;div&gt; @Html.LabelFor("UserName") @Html.TextBox("UserName")&lt;/div&gt; } </code></pre> <p>Controller</p> <pre><code>public ActionResult NewRegistration(RegistrationModel model) { if (Request.IsAjaxRequest()) { if (!ModelState.IsValid) { //Ques: How do I display this in ValidationSummary. return Json(new object[] { false, "Contains Email Address", ModelState.GetAllErrors() }); } else { if (UserNameIsTaken){ //Ques: This does not show in ValidationSummary ModelState.AddModelError("_FORM", "This username is already taken. Please pick a different username."); } } } else if (!ModelState.IsValid) { return View(model); } } </code></pre> <p>Model</p> <pre><code>public class RegistrationModel: IValidatableObject { [Required] //this gets displayed in ValidationSummary as Client-side validation public string UserName { get; set; } .. and all fields here } public IEnumerable&lt;ValidationResult&gt; Validate(ValidationContext context) { if (!string.IsNullOrEmpty(UserName) &amp;&amp; UserName.Contains(EmailAddress)) { yield return new ValidationResult("Username cannot contain email address", new[] { "UserName" }); } } </code></pre> <p>Web.config has </p> <blockquote> <p>UnobtrusiveJavaScriptEnabled="true" and ClientValidationEnabled="true"</p> </blockquote> <p>.js files included are</p> <blockquote> <p>script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></p> <p>script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></p> <p>script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></p> <p>script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></p> </blockquote> <p>What am I missing here? Any alternative? Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload