1

谁能帮助我如何在 ListBoxFor 中填充检索到的值。我试过这个但不起作用。

模型中的代码:-

public List<SelectListItem> SafetyRepresentataives { get; set; }
public List<string> BusinessUnitSiteSafetyRepresentative { get; set; }

控制器中的代码:=

var site = entityDB.ClientBusinessUnitSites.FirstOrDefault(m => m.ClientBusinessUnitSiteId == 
siteId);
var siteRepresentatives = from representatives in entityDB.ClientBusinessUnitSiteRepresentatives
                                      where representatives.ClientBUSiteID == siteId
                                      select representatives;

local.SafetyRepresentataives = (from o in entityDB.SystemUsersOrganizations.ToList()
                                           from c in entityDB.Contact.ToList()
                                           where o.OrganizationId == clientId && c.UserId == 
                                            o.UserId
                                           select new SelectListItem
                                           {
                                               Text = c.FirstName + "," + c.LastName,
                                               Value = c.UserId.ToString()
                                           }).ToList();
foreach (var representative in siteRepresentatives)
            {   
local.BusinessUnitSiteSafetyRepresentative.Add(representative.SafetyRepresentative.ToString());
            }

视图中的代码:-

@Html.ListBoxFor(x => Model.BusinessUnitSiteSafetyRepresentative, new 
MultiSelectList(Model.SafetyRepresentataives,"Value","Text"))

如上所示,我已从 DB 中检索到值。但我无法将这些检索到的值链接到 ListBox 以填充它们。这个怎么做??请帮帮我!!!!!!!!!

4

2 回答 2

0

我认为这个链接应该可以帮助你

http://www.asp.net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvc

在示例中,他们传递了 viewbag 中的组合框项目。

我建议您使用 jquery 插件,例如自动完成或 twitter bootstrap 中的 typeahead。所以你可以异步加载元素。

于 2013-06-11T20:20:54.907 回答
0

只需添加默认列表即可帮助我解决问题。这是解决方案。

local.BusinessUnitSiteSafetyRepresentative = new List<string>();
            foreach (var representative in siteRepresentatives)
            {

local.BusinessUnitSiteSafetyRepresentative.Add(representative.SafetyRepresentative.ToString());
            }
于 2013-06-19T09:57:52.513 回答