Change ModelState Value in MVC and .Net Core

Change or Reset a value in the ModelState using ASP.NET MVC and ASP.Net Core MVC inside [HttpPost] method

September 15, 2020

Sample Model

public class State
    {
        public int State_Id { getset; }
        public string State_Title { getset; }
    }
    public class City
    {
        public int City_Id { getset; }
        public string City_Title { getset; }
        public State state { getset }
    }

ASP.NET - MVC

ModelState.SetModelValue("City_Id"new ValueProviderResult("Change City ID"""CultureInfo.InvariantCulture));
ModelState.SetModelValue("City_Title"new ValueProviderResult("Change Title"""CultureInfo.InvariantCulture));
ModelState.SetModelValue("state.State_Id"new ValueProviderResult("Change State Id"""CultureInfo.InvariantCulture));
ModelState.SetModelValue("state.State_Title"new ValueProviderResult("Change Title"""CultureInfo.InvariantCulture));

ASP.NET - CORE MVC

ModelState.SetModelValue("City_Id"new ValueProviderResult("New Value"CultureInfo.InvariantCulture));
ModelState.SetModelValue("City_Title"new ValueProviderResult("New Value"CultureInfo.InvariantCulture));
ModelState.SetModelValue("state.State_Id"new ValueProviderResult("New Value"CultureInfo.InvariantCulture));
ModelState.SetModelValue("state.State_Title"new ValueProviderResult("New Value"CultureInfo.InvariantCulture));

Clear Or Reset ModelState

ModelState.Clear();

Post Comments(0)

Leave a reply

Will not be displayed in comment box .

Loading...