---
title: "How to use StringLength to limit the length of a string property in a model."  
description: "How to use StringLength to limit the length of a string property in a model."  
author: "Sandra Emily"  
published: 2023-10-20  
updated: 2023-10-22  
canonical: https://www.mindstick.com/forum/160244/how-to-use-stringlength-to-limit-the-length-of-a-string-property-in-a-model  
category: ".net core"  
tags: ["asp.net core", ".net core", "data annotations"]  
reading_time: 2 minutes  

---

# How to use StringLength to limit the length of a string property in a model.

How to use **StringLength** to [limit](https://www.mindstick.com/forum/159824/explain-the-purpose-of-the-limit-and-offset-clauses-and-how-are-they-used-for-pagination) the [length of a string](https://answers.mindstick.com/qa/104780/how-to-find-the-length-of-a-string) [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) in a model.

## Replies

### Reply by Aryan Kumar

You can use the **StringLength** data annotation in .NET Core to limit the [length](https://www.mindstick.com/interview/1915/what-is-the-difference-between-char_length-and-length) of a [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) property in a model. The **StringLength** attribute allows you to specify both the minimum and maximum length for a string property. Here's how to use it:

## Import the Required Namespace:

Ensure that you've imported the **System.ComponentModel.DataAnnotations** namespace, which contains the **StringLength** attribute.

```plaintext
using System.ComponentModel.DataAnnotations;
```

## Apply the StringLength Attribute:

Apply the **StringLength** attribute to the string property in your model class. You can set the **MinimumLength** and **MaximumLength** properties to define the acceptable range for the string's length.

```plaintext
public class Product
{
    [StringLength(50, MinimumLength = 3, ErrorMessage = "The product name must be between 3 and 50 characters.")]
    public string Name { get; set; }
}
```

In this example, the **Name** property is decorated with the **StringLength** attribute, which specifies that the minimum length is 3 characters and the maximum length is 50 characters. The **ErrorMessage** property is also set to provide a custom error message if the validation fails.

## Display Validation Errors in Views:

In your views, use the **ValidationMessageFor** HTML helper to display validation errors for the property.

```plaintext
<div class="form-group">
    <label asp-for="Name"></label>
    <input asp-for="Name" class="form-control" />
    <span asp-validation-for="Name" class="text-danger"></span>
</div>
```

This code will display the custom error message defined in the **StringLength** attribute when the string length is outside the specified range.

## Server-Side Validation:

In your controller action methods, you can check the **ModelState.IsValid** property to determine if the validation has passed. If **ModelState.IsValid** is **false**, you can handle validation errors as needed.

```plaintext
[HttpPost]
public IActionResult Create(Product product)
{
    if (!ModelState.IsValid)
    {
        // Handle validation errors, e.g., return to the form with error messages.
        return View(product);
    }

    // If validation is successful, proceed with the data.
    // ...
}
```

By using the **StringLength** data annotation, you can ensure that a string property in your model complies with the specified length constraints, providing meaningful validation error messages and enhancing the user experience.


---

Original Source: https://www.mindstick.com/forum/160244/how-to-use-stringlength-to-limit-the-length-of-a-string-property-in-a-model

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
