ASP.NET Core 8 MVC model validation – [Required] vs nullable type indicator

I’m trying to understand how model validation works as I finalize the design on my website using ASP.NET Core 8 MVC in Visual Studio. I’ve read several tutorials and posts on here, but the behavior I’m seeing doesn’t seem to match what I was expecting.

using System.ComponentModel.DataAnnotations;

namespace mywebsite.Models
{
    public class TestAModel
    {
        //[Required]
        //public int nonnullableint {  get; set; }
        //public int? nullableint {  get; set; }
        //public string? nullablestring { get; set; }
        //public string nonnullablestring { get; set; }
        //public TestCModel? nullablenonrequiredmodel { get; set; }
        public TestCModel nonnullablenonrequiredmodel { get; set; }
        //public TestBModel? nullablerequiredmodel { get; set; }
        //public TestBModel nonnullablerequiredmodel { get; set; }
        //public List<TestBModel>? nullablerequiredmodellist { get; set; }
        //public List<TestBModel> nonnullablerequiredmodellist { get; set; }
        //public List<TestCModel>? nullablenonrequiredmodellist { get; set; }
        //public List<TestCModel> nonnullablenonrequiredmodellist { get; set; }
    }
}
namespace mywebsite.Models
{
    public class TestCModel
    {
        public int? notrequiredint { get; set; }
    }
}

HomeController‘s Index() action method:

   public IActionResult Index()
   {
       TestAModel model = new TestAModel();

       //model.nonnullablenonrequiredmodel = new TestCModel();
       var validate = TryValidateModel(model);

       if (ModelState.IsValid)
       {
           var variabletest = 1;
       }

       return View();   // breakpoint here to view results
   }

What I’ve found, using TestCModel as an example, basically is this:

public class TestAModel
{
    //[Required]
    public TestCModel? nonnullablenonrequiredmodel { get; set; }
}

public IActionResult Index()
{
    TestAModel model = new TestAModel();
    //model.nonnullablenonrequiredmodel = new TestCModel();
    var validate = TryValidateModel(model);

    if (ModelState.IsValid)
    {
        var variabletest = 1;
    }

    return View();
}

This results in validation = true.

public class TestAModel
{
    [Required]
    public TestCModel? nonnullablenonrequiredmodel { get; set; }
}

public IActionResult Index()
{
    TestAModel model = new TestAModel();
    //model.nonnullablenonrequiredmodel = new TestCModel();

    var validate = TryValidateModel(model);

    if (ModelState.IsValid)
    {
        var variabletest = 1;
    }

    return View();
}

This results in validation = false.

public class TestAModel
{
    [Required]
    public TestCModel? nonnullablenonrequiredmodel { get; set; }
}

public IActionResult Index()
{
    TestAModel model = new TestAModel();
    model.nonnullablenonrequiredmodel = new TestCModel();

    var validate = TryValidateModel(model);

    if (ModelState.IsValid)
    {
        var variabletest = 1;
    }

    return View();
}

This again results in validation = true.

All of which makes sense to me, except then when I did this:

public class TestAModel
{
    //[Required]
    public TestCModel nonnullablenonrequiredmodel { get; set; }
}

public IActionResult Index()
{
    TestAModel model = new TestAModel();
    //model.nonnullablenonrequiredmodel = new TestCModel();

    var validate = TryValidateModel(model);

    if (ModelState.IsValid) 
    {
        var variabletest = 1;
    }

    return View();
}

I got validation = false!

I don’t understand this because aren’t classes inherently nullable? I also don’t understand this, because it seems to limit the value of the [Required] tag. What’s the point of it if something is required simply by leaving it non-nullable? I had the same results with string types as well.

The only thing interesting was int types, because the non-nullable int defaulted to a value = 0, so the way to make it required was [Required] and nullable.

Am I missing something super basic? Please help.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật