Html.DropDownListFor混淆
有人可以帮我理解Html.DropDownListFor的工作原理吗? 我的模型如下
public class TestModel { public IList ProductNames { get; set; } public string Product { get; set; } }
对DropDownListFor的调用看起来像
@Html.DropDownListFor(model => model.ProductNames, Model.ProductNames, "Select a Product", new {@class="selectproductname" })
通过此设置,我发现下拉列表已正确填充,但是在提交表单后,我似乎无法获得所选项目。 另外从我读过的Html.DropDownListFor的调用实际上应该是这样的
@Html.DropDownListFor(model => model.Product, Model.ProductNames, "Select a Product", new {@class="selectproductname" })
事实上,代码的其他部分看起来也是如此,但是当我这样做时,下拉列表没有被填充。 我在这里错过了什么吗?
几个附注:1)从另一个下拉列表中选择一个值之后会出现这个下拉列表的数量,所以我通过调用getJSON从数据库中获取数据来进行AJAX调用2)应用程序是一个MVC应用程序
非常感谢任何提供的帮助。 如果您需要任何其他信息来帮助我回答此问题,请与我们联系
编辑:这里有一些更多的细节
这是控制器中用于检索下拉数据的操作方法
[AcceptVerbs(HttpVerbs.Get)] public JsonResult LoadProductsBySupplier(string parentId) { var ctgy = this._categoryService.GetAllCategoriesByParentCategoryId(Convert.ToInt32(parentId)); List ctgyIds = new List(); foreach (Category c in ctgy) { ctgyIds.Add(c.Id); } var prods = this._productService.SearchProducts(categoryIds: ctgyIds, storeId: _storeContext.CurrentStore.Id, orderBy: ProductSortingEnum.NameAsc); products = prods.Select(m => new SelectListItem() { Value = m.Id.ToString(), Text = m.Name.Substring(m.Name.IndexOf(' ') + 1) }); var p = products.ToList(); p.Insert(0, new SelectListItem() { Value = "0", Text = "Select A Product" }); products = p.AsEnumerable(); //model.ProductNames = products.ToList(); return Json(products, JsonRequestBehavior.AllowGet); }
这是对控制器中的操作的JQuery调用
$("#Supplier").change(function () { var pID = $(this).val(); $.getJSON("CoaLookup/LoadProductsBySupplier", { parentId: pID }, function (data) { var select = $("#ProductNames"); select.empty(); if (pID != "0") { $.each(data, function (index, itemData) { select.append($('', { value: itemData.Value, text: itemData.Text })); }); } }); });
这是$ .each循环,当我使用model => model.Product时,即使在变量数据中返回数据也没有输入
第二种用法是正确的,但是当你使用时
@Html.DropDownListFor(model => model.Product, .....
您正在生成具有属性id="Product"
的 ,因此您需要更改脚本以引用具有此ID的元素
.... $.getJSON("CoaLookup/LoadProductsBySupplier", { parentId: pID }, function (data) { var select = $("#Product"); // change this selector select.empty(); ....
编辑
另一方面,您不一定需要在控制器方法中创建SelectList
,并且您可以将代码简化为
[AcceptVerbs(HttpVerbs.Get)] public JsonResult LoadProductsBySupplier(int parentId) { List ctgyIds = _categoryService.GetAllCategoriesByParentCategoryId(parentId).Select(c => c.ID).ToList(); var products= _productService.SearchProducts(categoryIds: ctgyIds, storeId: _storeContext.CurrentStore.Id, orderBy: ProductSortingEnum.NameAsc).AsEnumerable().Select(p => new { ID = p.ID, Text = p.Name.Substring(m.Name.IndexOf(' ') + 1) }); return Json(products, JsonRequestBehavior.AllowGet); }
和脚本
$("#Supplier").change(function () { var pID = $(this).val(); var select = $("#Product").empty().append($('').text('Select A Product')); if (pID == '0') { return; } // this should really be testing for null or undefined but thats an issue with your first select $.getJSON('@Url.Action("LoadProductsBySupplier", "CoaLookup")', { parentId: $(this).val() }, function (data) { $.each(data, function (index, item) { select.append($('').val(item.ID).text(item.Text); }); }); });
还要在脚本中注意$.getJSON
之前的if
子句 – 调用服务器然后决定忽略返回值没有多少意义
要在“编辑”页面中获取所选值,请尝试使用:
@Html.DropDownList("name", new SelectList(ViewBag.Product, "Id","Name", item.Id))
在这个item.Id是选定的值,ViewBag.Product你必须使用linq从你的产品填充它,例如在Controller中。