从两个表中填充一个下拉列表 [英] Populate single dropdown from two tables
问题描述
该方案是:
数据存储在数据库中的项目和邻里表都。
现在,我想填充项目编号和项目名称和neighbourhoodId和邻里的名字下拉列表。
Data is stored in database in project and neighbourhood tables both. Now, i want to populate dropdown with project id and project name and neighbourhoodId and neighbourhood name.
我是对的,现在通过viewBag发送这样的:
i am right now sending through viewBag like this:
ViewBag.NeighbourhoodId = new SelectList(allNeighbourhood(), "Id", "NeighbourhoodName");
视图页面上,让下拉填充是这样的:
on view page, getting dropdown populated like this:
@Html.DropDownList("Locations", ViewBag.NeighbourhoodId as SelectList, "Select a location")
现在,如何发送另一个viewBag在这个DROPDOWNLIST。
Now,how to send another viewBag in this dropdownList.
二疑问句,我的下拉列表是局部视图于是,我像这样将数据发送到局部视图:
Second ques, My dropdown is in partial view So, i am sending data to partial view like this:
@Html.Partial("_CommentBoxSection",new Neighbourhood())
如何发送新的项目())与邻里一起。有没有在那里我可以将它们的同时发送的任何局部过载。
我已经看到了关于这个称号一些职位,但它们是一些不同的东西
我最近尝试了这一点:
how to send new Project()) along with neighbourhood. Is there any overload of partial where i can send both of them. i have seen some posts relating to this title but they are something different I have recently tried this:
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
};
}
}
和视图页面上直接
@model @model IEnumerable<ProjectAndNeighbourhoodListItemViewModel>
@Html.DropDownList("Locations", Model.Select(m => m.ToSelectListItem()), "Select a location")
但得到System.ArgumentNullException值不能为空我没有code控制器中我一定要通过在控制器的东西太
but getting System.ArgumentNullException value cannot be null i have no code in controller do i have to pass something in controller too
推荐答案
不要使用ViewBag将值传递到您的视图,使用的ViewModels相反,它是干净多了。该视图模型也是创建SelectListItems的好地方。创建或在您的控制器映射视图模型。
Do not use ViewBag to pass values to your view, use ViewModels instead, it is much cleaner. The ViewModel is also a good place to create your SelectListItems. Create or map the ViewModel in your controller.
// 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
}
}
}
在您的Razor视图:
In your Razor View:
@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")
这篇关于从两个表中填充一个下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!