Viewmodel parameter being passed as null

Asked

Viewed 73 times

0

I am trying to validate my login form, but the "Modelstate.Isvalid" function is not performing the validation.

inserir a descrição da imagem aqui

According to the image above, when executing Action, the "Effectwindmodel" parameter is being passed as "null". Below is the contents 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; }
    }
}
  • How’s your url? it’s a structure similar to this http://localhost:50339/Dashboard/Viewmodel? Login="Meulogin"

1 answer

0


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

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

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

Still, if you really want to use String.Empty, you will need to use another attribute that converts String.Empty for nil:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.