首页 > 解决方案 > asp.net 远程属性验证

问题描述

这是我的Register View Model

 public class RegisterViewModel
{

    [Required(AllowEmptyStrings = false, ErrorMessage = "Bu alanı boş bırakmayınız.")]
    public string ADI { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "Bu alanı boş bırakmayınız.")]
    [DataType(DataType.Password)]
    [MinLength(6, ErrorMessage = "Parola en az 6 karakter olmalıdır!")]
    public string PAROLA { get; set; }

    [DataType(DataType.Password)]
    [System.ComponentModel.DataAnnotations.Compare("PAROLA", ErrorMessage = "Şifre Eşleşmedi!")]
    public string PAROLA_DOGRULA { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "Bu alanı boş bırakmayınız.")]
    [RegularExpression(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Yanlış mail,Lütfen doğru mail Giriniz")]
    [Remote("IsExistEmail", "Account", ErrorMessage = "Bu email daha önce kullanımıştır")]
    public string EMAIL { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "Bu alanı boş bırakmayınız.")]
    public string SOYADI { get; set; }
}

这是我的login View Model

public class LoginViewModel
{
    [Required(AllowEmptyStrings = false, ErrorMessage = "Bu alanı boş bırakmayınız.")]
    [DataType(DataType.Password)]
    [Remote("CheckLoginPassword", "Account",ErrorMessage ="Girdiğiniz Email kayıtlarımızda bulunamadı.")]
    public string PAROLA { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "Bu alanı boş bırakmayınız.")]
    [RegularExpression(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Yanlış mail,Lütfen doğru mail Giriniz")]
    [Remote("CheckLoginMail", "Account", ErrorMessage = "Bu email daha önce kullanımıştır")]
    public string EMAIL { get; set; }
}

User View model我在我看来使用它

 public class UserViewModel
{
    public RegisterViewModel Register { get; set; }
    public LoginViewModel Login { get; set; }
}

这是我的,View我用我的UserViewModel,我想validation Form用这个Views,它工作正常,但在我Controller有一个JsonResult动作并想检查电子邮件是否存在,问题是当我发送电子邮件以进行JsonResult操作时它是空的

这是我的View

@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @id = "registerForm" }))
                                            {
                                                @Html.ValidationSummary(true)


                                                @Html.ValidationMessage("RegisterErrors", new { @class = "ValidationErrorMessageColor" })

                                                @Html.EditorFor(model => model.Register.ADI, new { htmlAttributes = new { required = "", @class = "myRegisterInput", placeholder = "Ad" } })
                                                @Html.ValidationMessageFor(model => model.Register.ADI, "", new { @class = "ValidationErrorMessageColor" })

                                                @Html.EditorFor(model => model.Register.SOYADI, new { htmlAttributes = new { required = "", @class = "myRegisterInput", placeholder = "Soyad" } })
                                                @Html.ValidationMessageFor(model => model.Register.SOYADI, "", new { @class = "ValidationErrorMessageColor" })

                                                @Html.EditorFor(model => model.Register.EMAIL, new { htmlAttributes = new { required = "", @class = "myRegisterInput", placeholder = "Email" } })
                                                @Html.ValidationMessageFor(model => model.Register.EMAIL, "", new { @class = "ValidationErrorMessageColor" })

                                                @Html.EditorFor(model => model.Register.PAROLA, new { htmlAttributes = new { required = "", @class = "myRegisterInput", placeholder = "Parola" } })
                                                @Html.ValidationMessageFor(model => model.Register.PAROLA, "", new { @class = "ValidationErrorMessageColor" })

                                                @Html.EditorFor(model => model.Register.PAROLA_DOGRULA, new { htmlAttributes = new { required = "", @class = "myRegisterInput", placeholder = "Parolayı onayla" } })
                                                @Html.ValidationMessageFor(model => model.Register.PAROLA_DOGRULA, "", new { @class = "ValidationErrorMessageColor" })

                                                <div class="sign-up">
                                                    <input type="submit" value="Hesap oluştur" />
                                                </div>
                                            }

这是我的JsonResult操作,当我发送电子邮件时它是空的

 public JsonResult IsExistEmail(string email)
    {
        return Json(!dbContext.KULLANICIs.Any(e => e.EMAIL == email), JsonRequestBehavior.AllowGet);
    }

另外我的表格在popup modal.

标签: asp.net-mvcjsonresultremote-validation

解决方案


推荐阅读