标签: html.listboxfor

如何使用DropdownList助手正确创建MultiSelect <select>?

(对不起,这里有几个项目,但似乎没有一个允许我让这个工作.)

我想创建一个允许多选的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

28
推荐指数
1
解决办法
3万
查看次数

如何在Mvc3中填充ListBoxFor?

我有一个类型存储过程列表,其中包含ID和Name作为数据.我在模型中具有int类型的属性和相同存储过程的列表.现在我想将这些信息绑定到ListBoxFor

在视图中我写了这个

@Html.ListBoxFor(x => x.HobbyId, new MultiSelectList(Model.listHobby, "pkHobbyId", "Hobby"))
Run Code Online (Sandbox Code Playgroud)

但我收到一个错误

当允许多个选择时,参数'expression'必须求值为IEnumerable.

请帮助如何绑定.

asp.net-mvc-3 html.listboxfor

8
推荐指数
2
解决办法
2万
查看次数

ListBox for ArgumentNullException参数名称:source

建立:

我使用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

7
推荐指数
1
解决办法
1570
查看次数

MVC方法从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)

asp.net-mvc html-helper asp.net-mvc-3 html.listboxfor

2
推荐指数
1
解决办法
4518
查看次数

@ Html.ListBox用于使用和创建问题

我有一个带有2个列表框(list1,list2)的网页,我可以将项目从一个列表框转移到另一个列表框并返回.当我单击提交按钮时,我想将list2中的所有项值返回到httppost方法.

我有3个问题:

  1. 如何确定所选列表的属性?
  2. 如何获得一个初始的空列表(对于list2)?list1是否填充了项目?
  3. 单击提交按钮时如何将所有list2项返回到模型?

模型:

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)

asp.net-mvc razor html.listboxfor

1
推荐指数
1
解决办法
4万
查看次数

带集合的模型 - Html.ListBoxFor不设置选定的项目

这个让我发疯,在我失去理智之前请帮忙.

问题摘要:我的模型"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

1
推荐指数
1
解决办法
8876
查看次数

VB.NET - 使用ListBoxFor和DropDownList

我是这个ASP.net MVC的新手,并且真的陷入了ListBoxFor和DropDownListFor.

我该如何使用它们?任何例子?

vb.net asp.net-mvc html.listboxfor

0
推荐指数
1
解决办法
7925
查看次数