我收到此错误“CS1928:
'System.Web.Mvc.HtmlHelper'确实如此 不包含'DropDownListFor'的定义和最佳扩展名 方法过载 “System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>中 System.Collections.Generic.IEnumerable)” 有一些无效的参数“
我的控制器看起来像这样
public class IndexController : Controller
{
public ActionResult Index()
{
EpfeSelectScreen model = new EpfeSelectScreen();
var b = (from a in dbEntitiesErste.CONFIG_APPLICATIONS
orderby a.APPLICATION_ID
select new EPFE.CustomDataObjects.CustomObjects
{
Text = a.APPLICATION_NAME,
Value = a.APPLICATION_ID
});
model.Application = b.OrderBy(x => x.Text).ToList();
return View(model);
}
}
我的模特就是这个
public class EpfeSelectScreen
{
public string Search { get; set; }
public string selectedApplication { get; set; }
public List<SelectListItem> Country { get; set; }
public List<CustomObjects> Application { get; set; }
public List<SelectListItem> MetaData { get; set; }
public List<SelectListItem> References { get; set; }
public List<SelectListItem> ReferencedBy { get; set; }
public List<SelectListItem> TreeView { get; set; }
public EpfeSelectScreen()
{
Country = new List<SelectListItem>();
Application = new List<CustomObjects>();
References = new List<SelectListItem>();
ReferencedBy = new List<SelectListItem>();
TreeView = new List<SelectListItem>();
}
}
我的自定义对象就是这个
public class CustomObjects
{
public string Text { get; set; }
public short Value { get; set; }
}
我的model.Application中有一条记录,但是当该数据传递给我的View时,我得到了这个错误。
我的视图看起来像这样
@model EPFE.Controllers.EpfeSelectScreen
@Html.DropDownListFor(m => m.selectedApplication, Model.Application)
如何解决这个问题?我究竟做错了什么?当我尝试使用ListBoxFor时,我得到相同的错误。
答案 0 :(得分:5)
DropDownListFor
时, SelectList
会将List
作为第二个参数。
您可以拥有以下内容:
@Html.DropDownListFor(m => m.selectedApplication,
new SelectList(Model.Application,"Value","Text"))
答案 1 :(得分:0)
对于添加要下拉的项目,您可以使用ViewData将值传递到下拉列表。
控制器中的: 你可以有像
这样的场景 IList<SelectListItem> SelectedOptions = new List<SelectListItem>();
IList<Options> DropDownOptions = new List<Options>();
//Add items to this shipping
foreach (var item in DropDownOptions)
{
SelectListItem sliItem = new SelectListItem();
string optionText = item._name;
sliItem.Value = item._value.ToString();
sliItem.Text = optionText;
SelectedOptions.Add(sliItem);
}
ViewData["Option"] = SelectedOptions;
在视图中您只需使用
即可 @Html.DropDownList("Option", ViewData["Option"] as SelectList)
在提交表单时,如果您正在使用该类,请记住在类中放置一个带有name选项的字符串属性,以便在提交表单时可以在控制器中使用它也是选定的值。