为动态html控件(MVC 3)创建验证?

时间:2013-02-01 18:57:04

标签: asp.net-mvc-3 validation

使用MVC 3,

我有一个创建动态控件的问题表单。我需要为这些控件添加以下验证:

01 - Unicode String. No line breaks or tabs. DATA_MAX_LENGTH applies.
02 - Unicode Memo. Line breaks permitted. No tabs. DATA_MAX_LENGTH applies.
03 - Non-negative integer, as string.
04 - Non-negative Money (precision 18,2), as string.
05 - Date, as string, in the U.S. 4-digit-year format (e.g. 12/31/2012).
06 - Boolean (aka "Bit", "Yes/No"), as string, with "1" for True, "0" for False.

这些值与表(sql server)中每个问题项的控件类型一起存储。

不确定如何将这些验证与我当前的代码结合使用?

public static string DisplayControl(this HtmlHelper helper, int controlId, string choices, string answer, int? controlType, int maxlength, int dataType)
        {
            var output = new StringBuilder();

            output.Append(@"<div class='answer'>");
            switch ((ControlTypes)controlType)
            {
                case ControlTypes.Calendar:
                    output.Append(String.Format("<input class='datepicker' type='text' id='{0}' name='{0}' value='{1}' />", controlId, answer));
                    break;
                case ControlTypes.DropDownList:
                    output.Append(String.Format("<select id='{0}' class='droplist' name='{0}'>", controlId));
                    var ddls = LookupList(choices, "|");
                    foreach (var ddl in ddls)
                    {
                        string selected = ddl.Equals(answer) ? "selected" : "";
                        output.Append(String.Format("<option value='{0}' class='dd_option' {1}>{0}</option>", ddl, selected));
                    }
                    output.Append("</select>");
                    break;
                case ControlTypes.RadioButtonList:
                    var rbls = LookupList(choices, "|");
                    output.Append("<fieldset class='radio_btns'>");
                    foreach(var rbl in rbls)
                    {
                        string rchecked = rbl.Equals(answer) ? "checked" : "";
                        output.Append(String.Format("<label><input class='qa_radio' type='radio' id='{1}' name='{0}' value='{1}' {2} />{1}</label>&nbsp;", controlId, rbl, rchecked)); 
                    }
                    output.Append("</fieldset>");
                    break;
                case ControlTypes.TextBoxMultiLineInteger:
                    output.Append(String.Format("<textarea id='{0}' name='{0}' class='multi_text'>{1}</textarea>", controlId, answer));
                    break;
                case ControlTypes.TextBoxMultiLineMaxLength:
                    output.Append(String.Format("<textarea id='{0}' name='{0}' class='multi_text'>{1}</textarea>", controlId, answer));
                    break;
                case ControlTypes.TextBoxSingleLineInteger:
                    output.Append(String.Format("<input class='single_line' type='number' id='{0}' name='{0}' value='{1}' {2} />", controlId, answer, maxlength > 0 ? String.Format("maxlength='{0}'", maxlength) : ""));
                    break;
                case ControlTypes.TextBoxSingleLineMaxLength:
                    output.Append(String.Format("<input class='single_line' type='text' id='{0}' name='{0}' value='{1}' {2} />", controlId, answer, maxlength > 0 ? String.Format("maxlength='{0}'", maxlength) : ""));
                    break;
            }

            output.Append("</div>");
            return output.ToString();
        }

0 个答案:

没有答案