Populate single dropdown from two tables

。_饼干妹妹 提交于 2019-12-08 15:07:13

问题


The scenario is:

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.

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")

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
        };
      }  
    }

and on view page directly,

 @model @model IEnumerable<ProjectAndNeighbourhoodListItemViewModel>
 @Html.DropDownList("Locations", Model.Select(m => m.ToSelectListItem()), "Select a location")

but getting System.ArgumentNullException value cannot be null i have no code in controller do i have to pass something in controller too


回答1:


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
        }
    }
}

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")


来源:https://stackoverflow.com/questions/34412574/populate-single-dropdown-from-two-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!