# 1.5.1.Adding validation

* 1.Add data annotation to the model

```
    public class Customer
    {
        public int Id{ get; set; }

        [Required]
        [StringLength(255)]
        public string Name { get; set; }

        public bool IsSubscribeToNesletter { get; set; }

        public MembershipType membershipType { get; set; }

        [Display(Name = "Membership Type")]
        public byte membershipTypeId { get; set; }

        [Display(Name = "Date or Birth")]
        public string Birthday { get; set; }
    }
```

* 2.Add ModelState in the controller

```
    public ActionResult Save(Customer customer)
    {
            if (!ModelState.IsValid)
            {
                var viewModel = new CustomerFormViewModel
                {
                    customer = customer,
                    MembershipTypes = _context.membershipType.ToList()
                };
                return View("CustomerForm", viewModel);
            }
```

* 3.Add warning message to the view

```
    <div class = "form-group">
        @Html.LabelFor(m => m.customer.Name)
        @Html.TextBoxFor(m => m.customer.Name, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.customer.Name);
    </div>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jen-hsuan-hsieh.gitbook.io/asp-net/chapter1/1.1.the-complete-asp.net-mvc-5-course/15implement-validation/151adding-validation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
