mvc3 - ajax formulaire de soumission et validation côté serveur

Je suis sûr que cela a été demandé déjà, mais j'ai essayé toutes les solutions, mais aucune de la solution donnée est la résolution de mon problème. Probablement, je ne suis pas la mise en œuvre de ce droit.

Je suis à l'aide de MVC3, rasoir, jQuery.

J'ai un nouveau formulaire d'inscription. Une fois l'inscription réussie, je veux afficher le message de succès (différents type d'enregistrement) dans une boîte de dialogue.

Si il y a une erreur avec ModelState, signifie ModelState.IsValid == faux, je veux afficher les erreurs dans ValidationSummary.

Vue

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, '<br />');
    $('#dialog').html(msg);
    $('#dialog').dialog('open');
}
@model Models.RegistrationModel
@using (Ajax.BeginForm("NewRegistration", "Account", new AjaxOptions() { OnBegin="OnBegin", OnSuccess = "OnSuccess", OnFailure = "OnFailure" }))
{ 
  @Html.ValidationSummary()
    <div>
    Register<br />
    @Html.RadioButton("RegistrationType ", "0", true) New Algebra class<br />
    @Html.RadioButton("RegistrationType ", "1") New user registration by email<br />
    @Html.RadioButton("RegistrationType ", "2") New user registration by mailing address<br/>
   ..some more types
</div>
<div>
    here I display/hide controls based on Registration type selected.
        <div>
    @Html.LabelFor("UserName")
    @Html.TextBox("UserName")</div>
}

Contrôleur

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

}

Modèle

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<ValidationResult> Validate(ValidationContext context)
{
        if (!string.IsNullOrEmpty(UserName) && UserName.Contains(EmailAddress)) {
            yield return new ValidationResult("Username cannot contain email address", new[] { "UserName" });
        }
    }

Web.config a

UnobtrusiveJavaScriptEnabled="true" et ClientValidationEnabled="true"

.js fichiers inclus sont

script src="@Url.Contenu("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript">

script src="@Url.Contenu("~/Scripts/jquery.validate.min.js")" type="text/javascript">

script src="@Url.Contenu("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript">

script src="@Url.Contenu("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript">

Ce qui me manque ici? Toute autre alternative?
Merci à l'avance.

OriginalL'auteur peacefulmember | 2012-01-11