Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8698587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:45:40+00:00 2026-06-13T01:45:40+00:00

Controller Action: [AjaxRequestOnly, HttpPost, Authorize] public ActionResult AddCreditCard(CustomerCreditCardModelView cc) { if (!ModelState.IsValid) { Response.StatusCode

  • 0

Controller Action:

 [AjaxRequestOnly, HttpPost, Authorize]
    public ActionResult AddCreditCard(CustomerCreditCardModelView cc) {
        if (!ModelState.IsValid) {
            Response.StatusCode = 400;
            return Content(Newtonsoft.Json.JsonConvert.SerializeObject(ModelState.Values.SelectMany(v => v.Errors).Select(e=>e.ErrorMessage)));
        }
        // .... do something ...        
    }

ModelView:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using CheckoutVC.CheckoutServiceReference;
using Newtonsoft.Json;

public class CustomerCreditCardModelView {

    [Display(Name = "Número")]
    [Required(ErrorMessage = "El Número es requerido")]
    [StringLength(20, MinimumLength = 12, ErrorMessage = "El número parece ser incorrecto")]
    [JsonIgnore, ScriptIgnore]
    public string CardNumber { get; set; }

    [Display(Name = "Código de seguridad")]
    [Required(ErrorMessage = "El Código de seguridad es requerido")]
    public string CardSecurityCode { get; set; }

    [Display(Name = "Nombre en la tarjeta")]
    [Required(ErrorMessage = "El Nombre en la tarjeta es requerido")]
    public string NameOnCard { get; set; }

    [Display(Name = "Dirección de cobro")]
    [Required(ErrorMessage = "La Dirección de cobro es requerida")]
    public string BillingAddress { get; set; }

    [Display(Name = "Mes de vencimiento")]
    [Required(ErrorMessage = "El Mes de vencimiento es requerido")]
    public int ExpirationMonth { get; set; }

    [Display(Name = "Año de vencimiento")]
    [Required(ErrorMessage = "El Año de vencimiento es requerido")]
    public int ExpirationYear { get; set; }

    [Display(Name = "Documento")]
    [Required(ErrorMessage = "El documento es requerido")]
    public string Document { get; set; }

    [Display(Name = "Tipo de tarjeta")]
    public int IdCreditCard { get; set; }

    public int IdCustomer { get; set; }
    public bool IsDeleted { get; set; }
    public int IdCustomerCreditCard { get; set; }
    public CustomerCreditCardModelView() {

    }

Request Payload:

{"idCreditCard":1,"nameOnCard":"fdsa","billingAddress":"fdsa"}

Request Response:

["El Número es requerido","El Código de seguridad es requerido","El documento es requerido"]

As you can see, everything works perfectly except that int ExpirationMonth and int ExpirationYear should return a validation error (required) but they doesnt.

Actually, modelstate have only 6 keys….

What can be causing this behaviour? I want to expirationMonth and expirationYear takes into consideration the [Required] DataAnnotation attribute.

Pretty sure is the same happening here: mvc model validation required not working on all fields

EDIT: Reading the comments on that question see that the problem was the using, not my case tho. I included the using part.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:45:41+00:00Added an answer on June 13, 2026 at 1:45 am

    This is probably because ExpirationMonth and ExpirationYear properties are integer values. Integer is value type so it cannot be null, the default value will be 0 which is correct from the RequiredAttribute’s point of view.

    What you can do is to change the type from int to int? so it can be null.

    See Nullable Types.

    Your viewmodel:

    public class CustomerCreditCardModelView {
    
        [Display(Name = "Número")]
        [Required(ErrorMessage = "El Número es requerido")]
        [StringLength(20, MinimumLength = 12, ErrorMessage = "El número parece ser incorrecto")]
        [JsonIgnore, ScriptIgnore]
        public string CardNumber { get; set; }
    
        [Display(Name = "Código de seguridad")]
        [Required(ErrorMessage = "El Código de seguridad es requerido")]
        public string CardSecurityCode { get; set; }
    
        [Display(Name = "Nombre en la tarjeta")]
        [Required(ErrorMessage = "El Nombre en la tarjeta es requerido")]
        public string NameOnCard { get; set; }
    
        [Display(Name = "Dirección de cobro")]
        [Required(ErrorMessage = "La Dirección de cobro es requerida")]
        public string BillingAddress { get; set; }
    
        [Display(Name = "Mes de vencimiento")]
        [Required(ErrorMessage = "El Mes de vencimiento es requerido")]
        public int? ExpirationMonth { get; set; }
    
        [Display(Name = "Año de vencimiento")]
        [Required(ErrorMessage = "El Año de vencimiento es requerido")]
        public int? ExpirationYear { get; set; }
    
        [Display(Name = "Documento")]
        [Required(ErrorMessage = "El documento es requerido")]
        public string Document { get; set; }
    
        [Display(Name = "Tipo de tarjeta")]
        public int IdCreditCard { get; set; }
    
        public int IdCustomer { get; set; }
        public bool IsDeleted { get; set; }
        public int IdCustomerCreditCard { get; set; }
        public CustomerCreditCardModelView() {
    
        }
    }
    

    Another approach is to make the validation more strict, from the names ExpirationMonth and ExpirationYear I think the RangeAttribute is appropriate for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Controller action: public ActionResult Index() { ViewData[sample] = DateTime.Now.ToLongTimeString(); Thread.Sleep(3000); return View(); } View
My Controller Action is: public ActionResult PVInPage() { return View(); } public ActionResult ViewPage2()
my controller action: [HttpPost] public ActionResult AddPointAndCopyOtherSongToPlaylist(int id) { if (CheckIfAddPointToSelf(User.Identity.Name, id)) { var
I have a POST controller action like: if (ModelState.IsValid) { try { //.. save
Controller action: public ActionResult Index() { probaEntities proba = new probaEntities(); probaEntities proba1 =
I have a controller action like this: public ActionResult Index(string url) { var pageTitle
let say your controller action looks like this: public ActionResult Update(Car myCar) { }
I have a controller action which renders a partial view: public ActionResult Details(int id)
I have the following controller action: public ActionResult Details(string pk) { IEnumerable<ContentDetail> model =
In my Controller Action I set Cookie with Response.SetCookie(myCookie) method. In my Unit Test

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.