validating cascading dropdownlist
i am working on MVC.Net. in that i have used cascading dropdownlist. I want to do validations for blank field.
the view page coding is:
Select Category: <%= Html.DropDownList("Makes", ViewData["Makes"] as SelectList,"Select Category")%> Select Subcategory: <%= Html.CascadingDropDownList("Models", "Makes")%>
the code on controller:
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");
}
and for that i have made one class:
public static class JavaScriptExtensions { public sta开发者_JAVA百科tic string CascadingDropDownList(this HtmlHelper helper, string name, string 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; }
}
but when i run the aaplication it gives me following error:
Server Error in '/' Application. The parameters dictionary contains a null entry for parameter 'Models' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult AddSubCategoryPage(Int32, System.String, System.String)' in 'CMS.Controllers.HomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters .
plz help me.
The Models of type CascadingSelectList is null. Make sure an instance of the object exists and properties have no nulls. Also I would recommend using JSON to build your drop downs as bundling the combo arrays to return can create a performance hit and is confusing.
<script type="text/javascript">
$(function() {
$.getJSON("/Home/Make/List", function(data) {
var items = "<option>---------------------</option>";
$.each(data, function(i, make) {
items += "<option value='" + make.Value + "'>" + make.Text + "</option>";
});
$("#Make").html(items);
});
$("#Make").change(function() {
$.getJSON("/Home/Models/List/" + $("#Make > option:selected").attr("value"), function(data) {
var items = "<option>---------------------</option>";
$.each(data, function(i, model) {
items += "<option value='" + model.Value + "'>" + model.Text + "</option>";
});
$("#Model").html(items);
});
});
});
</script>
精彩评论