英文:
How to populate the dropdown list from database in mvc? Need to show names from database in dropdown
问题
I'm providing the translated code snippets as requested:
Controller:
[HttpGet]
public ActionResult PopulateDropDown()
{
List<SiteListViewModel> names = _db.Sites.Select(n => new SiteListViewModel { SiteId = n.SiteId, SiteName = n.Name }).ToList();
ViewBag.All = names;
return PartialView("_Layout", names);
}
View:
@Html.DropDownList("SiteId", ViewBag.All as SelectList, new { @class = "form-control" })
Model:
public class SiteListViewModel
{
public int SiteId { get; set; }
public string SiteName { get; set; }
public IEnumerable<SelectListItem> SiteNames { get; set; }
}
Please let me know if there's anything else you need assistance with.
英文:
I tried quite some things but always got error "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'SiteId'."
I want it to populate from database in my layout partial page.
Controller
[HttpGet]
public ActionResult PopulateDropDown()
{
//var sitename = GetSites();
//SiteListViewModel slvm = new SiteListViewModel();
List<SiteListViewModel> names = _db.Sites.Select(n => new SiteListViewModel { SiteId = n.SiteId, SiteName = n.Name }).ToList();
ViewBag.All = names;
return PartialView("_Layout", names);
}
View
@Html.DropDownList("SiteId", ViewBag.All as SelectList, new { @class = "form-control" })
Model
public class SiteListViewModel
{
public int SiteId { get; set; }
public string SiteName { get; set; }
public IEnumerable<SelectListItem> SiteNames { get; set; }
}
答案1
得分: 0
Firstly, if you're passing the list as part of your model, there's no need to add it to ViewBag too...
首先,如果您将列表作为模型的一部分传递,就不需要将其添加到 ViewBag 中...
Secondly, you've got List<SiteListViewModel>
as your model, but the DropDown component requires you to have a list of List<SelectListItem>
...
其次,您的模型是 List<SiteListViewModel>
,但下拉组件要求您拥有一个 List<SelectListItem>
列表...
If you want to simply flatten multiple lists to a single list use the following:
如果您想要将多个列表简单地合并为一个列表,请使用以下方法:
Model.SelectMany(x => x.SiteNames).ToList()
remember to add @using System.Linq
to the top of your view.
Model.SelectMany(x => x.SiteNames).ToList()
记得在视图顶部添加 @using System.Linq
。
Once you've got this, you can do the following on your Razor page:
一旦您完成了这一步,您可以在 Razor 页面上执行以下操作:
@Html.DropDownListFor(m => m.SiteId, Model.SelectMany(x => x.SiteNames).ToList(), "Placeholder Text goes here.")
英文:
Firstly, if you're passing the list as part of your model, there's no need to add it to ViewBag too...
Secondly, you've got List<SiteListViewModel>
as your model, but the DropDown component requires you to have a list of List<SelectListItem>
...
If you want to simply flatten multiple lists to a single list use the following:
Model.SelectMany(x => x.SiteNames).ToList()
remember to add @using System.Linq
to the top of your view.
Once you've got this, you can do the following on your Razor page:
@Html.DropDownListFor(m => m.SiteId, Model.SelectMany(x => x.SiteNames).ToList(), "Placeholder Text goes here.")
答案2
得分: 0
我在进行这些更改后可以得到它。通过在视图和模型中都保留由selectlistitem传递的列表。
控制器
List<SelectListItem> names = _db.Sites.Select(n => new SelectListItem { Text = n.SiteId.ToString(), Value = n.Name }).ToList();
视图
@Html.DropDownList("All", (IEnumerable<SelectListItem>)ViewBag.All)
英文:
I could get it after making these changes. By keeping the list passed by selectlistitem in both view and model.
Controller
List<SelectListItem> names = _db.Sites.Select(n => new SelectListItem { Text = n.SiteId.ToString(), Value = n.Name }).ToList();
View
@Html.DropDownList("All", (IEnumerable<SelectListItem>)ViewBag.All)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论