(对不起,这里有几个项目,但似乎没有一个允许我让这个工作.)
我想创建一个允许多选的DropDownList.我能够填充列表,但我无法获得当前选定的值似乎工作.
我的控制器中有以下内容:
ViewBag.PropertyGroups = from g in db.eFinGroups
where g.GroupType.Contents == "P"
select new
{
Key = g.Key,
Value = g.Description,
Selected = true
};
ViewBag.SelectedPropertyGroups = from g in company.Entities
.First().Properties.First().PropertyGroups
select new {
g.eFinGroup.Key,
Value = g.eFinGroup.Description };
在视图中我有:
@Html.DropDownListFor(model => model.PropertyGroupsX,
new MultiSelectList(ViewBag.PropertyGroups
, "Key", "Value"
, ViewBag.SelectedPropertyGroups),
new { @class = "chzn-select", data_placeholder = "Choose a Property Group", multiple = "multiple", style = "width:350px;" })
PropertyGroupX是模型中的字符串[].
我已尝试使用所选属性的所有类型的迭代...只传递值,只传递键,两者等.
另外,PropertyGroupX应该是什么类型的?字符串数组是否正确?或者它应该是包含当前属性组的字典?我真的很难找到这方面的文档.
有人建议我应该使用ListBoxFor.我已经改变了,仍然有同样的问题.呈现选项标记时,所选值未设置为选中状态.这是我尝试过的:
@ Html.ListBoxFor(model => model.PropertyGroups,new MultiSelectList(ViewBag.PropertyGroups,"Key","Value"))
我已经尝试将model.PropertyGroups作为匹配值的字符串集合,作为Guid的集合匹配此ID,并作为匿名类型同时具有Key和Value以匹配ViewBag中的项目.似乎没什么用.
selectlist multi-select asp.net-mvc-3 drop-down-menu html.listboxfor
我有一个类型存储过程列表,其中包含ID和Name作为数据.我在模型中具有int类型的属性和相同存储过程的列表.现在我想将这些信息绑定到ListBoxFor
在视图中我写了这个
@Html.ListBoxFor(x => x.HobbyId, new MultiSelectList(Model.listHobby, "pkHobbyId", "Hobby"))
Run Code Online (Sandbox Code Playgroud)
但我收到一个错误
当允许多个选择时,参数'expression'必须求值为IEnumerable.
请帮助如何绑定.
我使用MvcScaffolding搭建了一个控制器.
对于一个属性Model.IdCurrencyFrom,脚手架创建了一个Html.DropDownListFor:
@Html.DropDownListFor(model => model.IdCurrencyFrom,
((IEnumerable<FlatAdmin.Domain.Entities.Currency>)ViewBag.AllCurrencies).Select(option => new SelectListItem {
Text = (option == null ? "None" : option.CurrencyName),
Value = option.CurrencyId.ToString(),
Selected = (Model != null) && (option.CurrencyId == Model.IdCurrencyFrom)
}), "Choose...")
Run Code Online (Sandbox Code Playgroud)
无论是使用新记录还是编辑现有记录,这都可以正常工作.
只有3种货币,AR $,US $和GB£.因此,我想要一个ListBox而不是下拉列表.
所以我把上面改为:
@Html.ListBoxFor(model => model.IdCurrencyFrom,
((IEnumerable<FlatAdmin.Domain.Entities.Currency>)ViewBag.AllCurrencies).Select(option => new SelectListItem {
Text = (option == null ? "None" : option.CurrencyName),
Value = option.CurrencyId.ToString(),
Selected = (Model != null) && (option.CurrencyId == Model.IdCurrencyFrom)
}))
Run Code Online (Sandbox Code Playgroud)
我现在得到一个ArgumentNullException,参数名称:source,但仅在编辑现有记录时.创建新记录,这很好.
怎么了?!
什么也没有变.切换回DropDownListFor,一切正常.切换到ListBox(而不是ListBoxFor),我得到错误.
该模型不为null(就像我说的,它适用于DropDownListFor)...而且我的想法已经用完了.
asp.net-mvc asp.net-mvc-3 asp.net-mvc-scaffolding html.listboxfor
在MVC3中,我想通过Html.ListBoxFor方法更改HTML输出,以便代替包含所有可用值的HTML列表框(以及突出显示的选定值),我想简单地输出无序列表(UL,LI)所选项目而不是SELECT元素.问题是我希望保持与ListBoxFor方法完全相同的方法签名,接受MultiSelectList对象和List,它是选定的值.然后,我希望无序列表仅输出所选项目值(而不是键)作为UL/LI html.这是我想要的方法签名.如何实现这一目标?
public static MvcHtmlString ListBoxForAsUnorderedList <TModel, TProperty>
(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
IEnumerable<SelectListItem> selectList)
Run Code Online (Sandbox Code Playgroud) 我有一个带有2个列表框(list1,list2)的网页,我可以将项目从一个列表框转移到另一个列表框并返回.当我单击提交按钮时,我想将list2中的所有项值返回到httppost方法.
我有3个问题:
模型:
public class TestModel
{
public List<SelectListItem> OptionList { get; set; }
public List<SelectListItem> SelectedOptionList { get; set; }
public string[] SelectedOptions { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
控制器:
private TestModel testModel = new TestModel();
public ActionResult TestPage()
{
ViewBag.Message = "Test Page";
testModel.OptionList = new List<SelectListItem>();
for (int i = 1; i <= 100; i++)
{
SelectListItem item = new SelectListItem();
item.Text = "Option " …Run Code Online (Sandbox Code Playgroud) 这个让我发疯,在我失去理智之前请帮忙.
问题摘要:我的模型"Thread"有"ForumMessage"集合,每个ForumMessage都有一个Multi select下拉列表.我想要做的就是根据来自数据库的值设置所选值.我已经通过很多线程,但无法找到解决方案.
如果您知道任何此类问题,请告诉我,我会通过他们.
以下是我的模特
public class Thread
{
public List<ForumMessage> Messages { get; set; }
//Master list coming from DB
public List<Classifications> AllClassifications { get; set; }
public string Subject { get; set; }
public int[] ThreadSelectedClassifications { get; set; }
}
public class ForumMessage
{
public string MessageName { get; set; }
public int[] SelectedClassifications { get; set; }
}
public class Classifications
{
public int ID { get; set; }
public string Title { get; set; } …Run Code Online (Sandbox Code Playgroud) c# html.dropdownlistfor .net-4.5 html.listboxfor asp.net-mvc-5
我是这个ASP.net MVC的新手,并且真的陷入了ListBoxFor和DropDownListFor.
我该如何使用它们?任何例子?
asp.net-mvc ×4
.net-4.5 ×1
c# ×1
html-helper ×1
multi-select ×1
razor ×1
selectlist ×1
vb.net ×1