Asp.net mvc walidacja - The value '' is invalid.

0

Używam takich data annotations

 
       public class LoginViewModel
    {
        [Required(ErrorMessage = "Podaj login")]
        [Display(Name = "Nazwa użytkownika")]
        public string Login { get; set; }
        [Required(ErrorMessage = "Podaj hasło")]
        [Display(Name = "Hasło")]
        [DataType(DataType.Password)]
        public string Password { get; set; }
        [Display(Name = "Zapamiętaj logowanie")]
        public bool RememberMe { get; set; }
    }

Kontroler:

        
        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(LoginViewModel login)
        {
            if (ModelState.IsValid)
            {
                return RedirectToAction("Index","Account");
            }
            return View(login);
        } 

Widok


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>LoginViewModel</h4>
        <hr />
        @Html.ValidationSummary(true)

        <div class="form-group">
            @Html.LabelFor(model => model.Login, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Login)
                @Html.ValidationMessageFor(model => model.Login)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Password, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Password)
                @Html.ValidationMessageFor(model => model.Password)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.RememberMe, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.RememberMe)
                @Html.ValidationMessageFor(model => model.RememberMe)
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}
 

i w widoku przy próbie wysłania danych zamiast zdefiniowanych błędów zawsze wyskakuje The value 'jakaswartosc' is invalid.
Dodam że mam zrobiony podobny formularz dla rejestracji i w nim wszystko działa, wie ktoś o co może chodzić?

0

Dodaj na stringi atrybut

[DisplayFormat(ConvertEmptyStringToNull=false)]

Btw. widzę, że bootstrap cieszy się popularnością ;)

0

sprawdź w Widoku pierwszą linijkę @model ... czy jest poprawny
"jakaswartość" czy to taki zapis czy to jakaś konkretna wartość?

0

Odkopuje wątek. Mam ten sam problem, że pokazuje mi się "The value 'gfd' is not valid for Phone." zamiast mojego tekstu zdefiniowanego w ErrorMessage.

Mój kod:

  [Required(ErrorMessage ="Proszę podać swoje imie i nazwisko.")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Proszę podać adres email.")]
        [RegularExpression(".+\\@.+\\..+", ErrorMessage = "Proszę podać prawidłowy email.")]
        public string Email { get; set; }

        /* [Required(ErrorMessage = "Proszę podać numer telefonu.")]
        // to moje [DataType(DataType.PhoneNumber)]
         public string Phone { get; set; } */

        [Required(ErrorMessage = "Proszę podać numer telefonu.")]
        [DataType(DataType.PhoneNumber)]
        [RegularExpression("[0-9]{9}", ErrorMessage = "Numer telefonu musi składać się z 9 cyfr.")] 
        public int? Phone { get; set; }

        [Required(ErrorMessage = "Proszę określić czy weźmiesz udział.")]
        public bool? WillAttend { get; set; } 
0

Zmieniłem inta na stringa w metodzie Phone i zadziałało, ale może mi ktoś wyjaśnić dlaczego int nie działał tak jak chciałem?

  [Required(ErrorMessage = "Proszę podać numer telefonu.")]
        [DataType(DataType.PhoneNumber)]
        [RegularExpression("[0-9]{9}", ErrorMessage = "Numer telefonu musi składać się z 9 cyfr.")] 
        public string Phone { get; set; }
1

'gfd' to nie jest dobra wartość dla int?, jak już zostałeś poinformowany. Wyrażenia regularne są do tekstu. Tutaj akurat masz "[0-9]{9}", ale co by się miało dziać, gdybyś podał np. "[a-z]{9}" dla tegoż inta?

1 użytkowników online, w tym zalogowanych: 0, gości: 1