本文介绍了如何设置文本框的边框颜色红验证失败时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在验证失败中我有一个任务来设置红色边框的文本框.NET MVC 4。

BExtensionMethods.cs

 公共静态字符串GetTextBoxColor(此ModelStateDictionary的ModelState)
    {
        字符串textBoxColor =的String.Empty;
        诠释计数= 1;
        VAR errorKeys =(从项目中的ModelState
                         其中,item.Value.Errors.Any()
                         选择item.Key).ToList();
        的foreach(在errorKeys VAR项)
        {
            textBoxColor + =的String.Format({0} {1}< / BR>中,伯爵,项目);
            算上++;
        }
        返回textBoxColor;
    }

下面的JSON对象包含values​​.How我可以过滤呢?


解决方案

 公共静态列表<串GT; GetTextBoxColor(这ModelStateDictionary的ModelState)
    {
        字符串textBoxColor =的String.Empty;
        诠释计数= 1;
        清单<串GT;名单=新名单,LT;串>();
        VAR errorKeys =(从项目中的ModelState
                         其中,item.Value.Errors.Any()
                         选择item.Key.Substring(item.Key.LastIndexOf()'。')修剪)了ToList()。('。')。
        的foreach(在errorKeys VAR项)
        {
            textBoxColor + =的String.Format({0} {1}< / BR>中,伯爵,项目);
            list.Add(项目);
            算上++;
        }
        返回列表;    }

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?

解决方案
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;

    }

这篇关于如何设置文本框的边框颜色红验证失败时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 20:37