验证级联下拉列表

时间:2010-04-29 09:24:27

标签: asp.net-mvc

我正在研究MVC.Net。因为我使用了级联下拉列表。我想对空白字段进行验证。

视图页面编码是:

                                                              选择类别:                                                       <%= Html.DropDownList(“制作”,ViewData [“制作”]为SelectList,“选择类别”)%>                                                                                                          选择子类别:                                                       <%= Html.CascadingDropDownList(“Models”,“Makes”)%>                              

控制器上的代码:

public ActionResult AddSubCategoryPage()          {

         var makeList = new SelectList(entityObj.Category.ToList(), "Category_id", "Category_name");
         ViewData["Makes"] = makeList;

         // Create Models view data
         var modelList = new CascadingSelectList(entityObj.Subcategory1.ToList(), "Category_id", "Subcategory_id", "Subcategory_name");
         ViewData["Models"] = modelList;

         return View("AddSubCategoryPage");
     }

为此我已经上了一堂课:

公共静态类JavaScriptExtensions     {         public static string CascadingDropDownList(此HtmlHelper助手,字符串名称,字符串associatedDropDownList)         {             var sb = new StringBuilder();

        // render select tag
        sb.AppendFormat("<select name='{0}' id='{0}'></select>", name);
        sb.AppendLine();

        // render data array
        sb.AppendLine("<script type='text/javascript'>");
        var data = (CascadingSelectList)helper.ViewDataContainer.ViewData[name];
        var listItems = data.GetListItems();
        var colArray = new List<string>();
        foreach (var item in listItems)
            colArray.Add(String.Format("{{key:'{0}',value:'{1}',text:'{2}'}}", item.Key, item.Value, item.Text));
        var jsArray = String.Join(",", colArray.ToArray());
        sb.AppendFormat("$get('{0}').allOptions=[{1}];", name, jsArray);
        sb.AppendLine();
        sb.AppendFormat("$addHandler($get('{0}'), 'change', Function.createCallback(bindDropDownList, $get('{1}')));", associatedDropDownList, name);
        sb.AppendLine();
        sb.AppendLine("</script>");

        return sb.ToString();

    }
}

public class CascadingSelectList
{
    private IEnumerable _items;
    private string _dataKeyField;
    private string _dataValueField;
    private string _dataTextField;

    public CascadingSelectList(IEnumerable items, string dataKeyField, string dataValueField, string dataTextField)
    {
        _items = items;
        _dataKeyField = dataKeyField;
        _dataValueField = dataValueField;
        _dataTextField = dataTextField;
    }

    public List<CascadingListItem> GetListItems()
    {
        var listItems = new List<CascadingListItem>();
        foreach (var item in _items)
        {
            var key = DataBinder.GetPropertyValue(item, _dataKeyField).ToString();
            var value = DataBinder.GetPropertyValue(item, _dataValueField).ToString();
            var text = DataBinder.GetPropertyValue(item, _dataTextField).ToString();
            listItems.Add(new CascadingListItem(key, value, text));
        }
        return listItems;
    }
}

public class CascadingListItem
{
    public CascadingListItem(string key, string value, string text)
    {
        this.Key = key;
        this.Value = value;
        this.Text = text;
    }

    public string Key { get; set; }
    public string Value { get; set; }
    public string Text { get; set; }
}

但是当我运行aaplication时它会给我以下错误:

'/'应用程序中的服务器错误。 参数字典包含'CMS.Controllers'中方法'System.Web.Mvc.ActionResult AddSubCategoryPage(Int32,System.String,System.String)'的非可空类型'System.Int32'的参数'Models'的空条目.HomeController”。可选参数必须是引用类型,可空类型,或者声明为可选参数。 参数名称:参数。

请帮助我。

1 个答案:

答案 0 :(得分:0)