csharp

How to change property name in MVC C#

In this article, we will learn how to change property name in MVC C#.

The Category Index page contains one column named Name as per Figure. This is because the view
uses the code @Html.DisplayNameFor(model => model.Name) to display the Name property of the
category.
change-property-name-mvc-c
change property name in MVC C#
To solve this problem, we can use a feature of DataAnnotations to add a display name attribute to the Name properties of the Category model classes.
Modify the Models\Category.cs file. Add this namespace first for how to change property name in MVC C#

using System.ComponentModel.DataAnnotations;

Use this [Display(Name = "Category Item Name”)] tells the MVC framework to display Category Item Name rather than Name when displaying in UI for the name of the property.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace ShopStore.Models
{
    public class Category
    {
        public int ID { get; set; }
        [D[Display(Name ="Category Item Name")]        public string Name { get; set; }
        public string Description { get; set; }
    }
}

Build the solution and run it. you can see now changes in heading.
change-property-name-mvc-c

2nd Ways: How to change property name in MVC C#
we can also change label name another way Splitting DataAnnotations into Another File Using MetaDataType.

Some person wants to the model classes as clean as possible, therefore preferring not to add DataAnnotations to them. This is achieved by using a MetaDataType class as follows.
Add a one new class to the Models folder called CategoryMetaData.cs and update the contents of the file to
the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using ShopStore.Models;

namespace ShopStore.Models
{
    [M[MetadataType(typeof(CategoryMetaData))]    public partial class Category
    {

    }
    public class CategoryMetaData
    {
        [D[Display(Name = "Category Item Name")]        public string Name;
    }
}

This declares the Category class as now being a partial class, meaning it is split across multiple files. The
DataAnnotation [M[MetadataType(typeof(CategoryMetaData))]s used to tell dot NET to apply metadata to the Category class from the CategoryMetaData class.
Modify the Category class back to its original state but declare category class as a partial class so that it can be used.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace ShopStore.Models
{
    public partial class Category
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
    }
}

you can see result is also same.
how-to-change-property-name-mvc-c

Leave a Reply

Your email address will not be published. Required fields are marked *