C# 从两个表中填充单个下拉列表
情况是: 数据存储在数据库中的项目表和邻里表中。 现在,我想用项目id和项目名称以及邻里id和邻里名称填充下拉列表 我现在通过viewBag发送如下内容:C# 从两个表中填充单个下拉列表,c#,asp.net-mvc,linq,partial-views,html.dropdownlistfor,C#,Asp.net Mvc,Linq,Partial Views,Html.dropdownlistfor,情况是: 数据存储在数据库中的项目表和邻里表中。 现在,我想用项目id和项目名称以及邻里id和邻里名称填充下拉列表 我现在通过viewBag发送如下内容: ViewBag.NeighbourhoodId = new SelectList(allNeighbourhood(), "Id", "NeighbourhoodName"); @Html.DropDownList("Locations", ViewBag.NeighbourhoodId as SelectList, "Select a l
ViewBag.NeighbourhoodId = new SelectList(allNeighbourhood(), "Id", "NeighbourhoodName");
@Html.DropDownList("Locations", ViewBag.NeighbourhoodId as SelectList, "Select a location")
@Html.Partial("_CommentBoxSection",new Neighbourhood())
在“查看”页面上,按如下方式填充下拉列表:
ViewBag.NeighbourhoodId = new SelectList(allNeighbourhood(), "Id", "NeighbourhoodName");
@Html.DropDownList("Locations", ViewBag.NeighbourhoodId as SelectList, "Select a location")
@Html.Partial("_CommentBoxSection",new Neighbourhood())
现在,如何在此下拉列表中发送另一个viewBag
第二,我的下拉列表是在部分视图中,因此,我向部分视图发送数据,如下所示:
ViewBag.NeighbourhoodId = new SelectList(allNeighbourhood(), "Id", "NeighbourhoodName");
@Html.DropDownList("Locations", ViewBag.NeighbourhoodId as SelectList, "Select a location")
@Html.Partial("_CommentBoxSection",new Neighbourhood())
如何将新项目()与邻居一起发送。有没有多余的部分,我可以发送他们两个。
我看过一些关于这个标题的帖子,但它们有些不同
我最近尝试了以下方法:
public class ProjectAndNeighbourhoodListItemViewModel
{
public Project Project { get; set; }
public Neighbourhood Neighbourhood { get; set; }
public string ProjectName { get; set; }
public int Id { get; set; }
public bool IsSelected { get; set; }
// public IEnumerable<SelectListItem> ToSelectListItem { get; set; }
public SelectListItem ToSelectListItem()
{
return new SelectListItem
{
Text = Project.ProjectName,
Value = Neighbourhood.Id.ToString(),
Selected = IsSelected
};
}
}
公共类项目和邻居列表项视图模型
{
公共项目{get;set;}
公共邻里{get;set;}
公共字符串ProjectName{get;set;}
公共int Id{get;set;}
公共布尔值被选为{get;set;}
//public IEnumerable ToSelectListItem{get;set;}
public SelectListItem到SelectListItem()
{
返回新的SelectListItem
{
Text=Project.ProjectName,
Value=neighbourth.Id.ToString(),
已选定=已选定
};
}
}
并直接在查看页面上
@model @model IEnumerable<ProjectAndNeighbourhoodListItemViewModel>
@Html.DropDownList("Locations", Model.Select(m => m.ToSelectListItem()), "Select a location")
@model@model IEnumerable
@Html.DropDownList(“位置”,Model.Select(m=>m.ToSelectListItem()),“选择位置”)
但是获取System.ArgumentNullException值不能为null我在控制器中没有代码我是否也必须在控制器中传递某些内容不要使用ViewBag将值传递到视图,而是使用ViewModels,这样更干净。ViewModel也是创建SelectListItems的好地方。在控制器中创建或映射ViewModel
// ViewModel for a single selectable entry
public class ProjectAndNeighbourhoodListItemViewModel {
public string ProjectName { get; set; }
public long NeighbourhoodId { get; set; }
public bool IsSelected { get; set; }
public SelectListItem ToSelectListItem() {
return new SelectListItem {
Text = ProjectName,
Value = NeighbourhoodId.ToString(),
Selected = IsSelected
}
}
}
在你看来:
@model IEnumerable<ProjectAndNeighbourhoodListItemViewModel>
@* Would be even better to use a wrapper model that contains the collection of list items as property! *@
@Html.DropDownList("Locations", Model.Select(m => m.ToSelectListItem()) , "Select a location")
@model IEnumerable
@*最好使用包含列表项集合作为属性的包装器模型*@
@Html.DropDownList(“位置”,Model.Select(m=>m.ToSelectListItem()),“选择位置”)
有人能告诉我怎么做吗??仍在等待帮助。您的问题不清楚。不能从一个选择列表中发布两个不同的值。如果希望用户选择项目和邻居,则需要两个选择列表。如果根据项目选择限制了社区,然后,您可以设置一组级联下拉列表,其中第一个select触发AJAX请求以过滤第二个列表。@ChrisPratt thnks用于您的评论和downvote,但我认为可以通过一个自定义列表实现,该列表包含两个表的数据,但不知道如何发布我的问题,我已在问题标题中明确说明从两个表中选择一个下拉列表,而不是从一个选择列表中选择。问题是,您不能。Post值是基于单个字符串的键值对。您只能发布select.thnks中的基本类型以供您使用,但在此viewmodel中没有项目Id。我想填充项目表和邻里表的下拉列表。bothDropDownList()只能获取一个可能选项的集合。为您的选项创建一个SelectList,但使用从两个表创建的SelectListItems填充它。thnk u我会尽快尝试