Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Required' error even after adding the required namespace

Look at the following code, I am using the required(System.ComponentModel.DataAnnotations) namespace but it keeps showing me red squiggly with the error that 'type or namespace name 'required' could not be found(are you missing a using directive.....)

using System.ComponentModel.DataAnnotations;

namespace SportsStore.Domain.Entities {

public class ShippingDetails {
    [Required(ErrorMessage = "Please enter a name")]
    public string Name { get; set; }

    [Required(ErrorMessage = "Please enter the first address line")]
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string Line3 { get; set; }

    [Required(ErrorMessage = "Please enter a city name")]
    public string City { get; set; }

    [Required(ErrorMessage = "Please enter a state name")]
    public string State { get; set; }

    public string Zip { get; set; }

    [Required(ErrorMessage = "Please enter a country name")]
    public string Country { get; set; }

    public bool GiftWrap { get; set; }
}
}
like image 962
Mohit Avatar asked Jun 08 '26 10:06

Mohit


2 Answers

Right click on project references. Select Add Reference.

In the .Net tab select System.ComponentModel.DataAnnotations

enter image description here

like image 101
Habib Avatar answered Jun 10 '26 23:06

Habib


Select the "Required" keyword and press Ctrl+">". It will show you the corresponding namespace name through intellisense. If the namespace is not found or not available, it will show you - "Generate Class ...".

like image 33
Umesh Avatar answered Jun 10 '26 22:06

Umesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!