ViewModel parameter being passed as null

0

I'm trying to validate my login form, but the "ModelState.IsValid" function is not performing validation.

Accordingtotheaboveimage,whenexecutingAction,the"MakeLoginViewModel" parameter is being passed as "null". Below is the content of my ViewModel.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HolisticWeb.Models;
using System.ComponentModel.DataAnnotations;

namespace HolisticWeb.ViewModel
{
    public class EfetuarLoginViewModel
    {
        public EfetuarLoginViewModel()
        {
            Login = String.Empty;
            Senha = String.Empty;
        }

        [Display(Name = "Nome de Usuário")]
        [Required(ErrorMessage = "Informe seu Login")]
        public string Login { get; set; }

        [Display(Name = "Senha")]
        [Required(ErrorMessage = "Informe sua Senha")]
        [DataType(DataType.Password)]
        public string Senha { get; set; }
    }
}
    
asked by anonymous 31.10.2015 / 23:08

1 answer

0

The problem is in the constructor, where you put a behavior that, in addition to not being necessary, disrupts the validator.

    public EfetuarLoginViewModel()
    {
        Login = String.Empty;
        Senha = String.Empty;
    }

[Required] evaluates correctly if the String is, but not empty. The correct one would be for you to just remove the constructor and test again.

Also, if you really want to use String.Empty , you will need to use another attribute that converts String.Empty to null:

    [DisplayFormat(ConvertEmptyStringToNull = true)]
    [Display(Name = "Nome de Usuário")]
    [Required(ErrorMessage = "Informe seu Login")]
    public string Login { get; set; }

    [DisplayFormat(ConvertEmptyStringToNull = true)]
    [Display(Name = "Senha")]
    [Required(ErrorMessage = "Informe sua Senha")]
    [DataType(DataType.Password)]
    public string Senha { get; set; }
    
31.10.2015 / 23:55