Search code examples
jqueryasp.netasp.net-mvc-4asp.net-mvc-3-areas

How to set the textbox border color red when validation fails


I have got a task to set red color border for text box when the validation fails in .net mvc 4.

BExtensionMethods.cs

    public static string GetTextBoxColor(this ModelStateDictionary ModelState)
    {
        string textBoxColor = string.Empty;
        int count = 1;
        var errorKeys = (from item in ModelState
                         where item.Value.Errors.Any()
                         select item.Key).ToList();
        foreach (var item in errorKeys)
        {
            textBoxColor += string.Format("{0}.{1}</br>", count, item);
            count++;
        }
        return textBoxColor;
    }

Here the json object contains the values.How can I filter it?


Solution

  • public static List<string> GetTextBoxColor(this ModelStateDictionary ModelState)
        {
            string textBoxColor = string.Empty;
            int count = 1;
            List<string> list = new List<string>();
            var errorKeys = (from item in ModelState
                             where item.Value.Errors.Any()
                             select item.Key.Substring(item.Key.LastIndexOf('.')).Trim('.')).ToList();
            foreach (var item in errorKeys)
            {
                textBoxColor += string.Format("{0}.{1}</br>", count, item);
                list.Add(item);
                count++;
            }
            return list;
    
        }