How to populate the dropdown list from database in mvc? Need to show names from database in dropdown

huangapple go评论81阅读模式
英文:

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&lt;SiteListViewModel&gt; names = _db.Sites.Select(n =&gt; new SiteListViewModel { SiteId = n.SiteId, SiteName = n.Name }).ToList();
            ViewBag.All = names;
            return PartialView(&quot;_Layout&quot;, names);       
        }

View

 @Html.DropDownList(&quot;SiteId&quot;, ViewBag.All as SelectList, new { @class = &quot;form-control&quot; })

Model

public class SiteListViewModel
    {
        public int SiteId { get; set; }
        public string SiteName { get; set; }

        public IEnumerable&lt;SelectListItem&gt; 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&lt;SiteListViewModel&gt; as your model, but the DropDown component requires you to have a list of List&lt;SelectListItem&gt;...

其次,您的模型是 List&lt;SiteListViewModel&gt;,但下拉组件要求您拥有一个 List&lt;SelectListItem&gt; 列表...

If you want to simply flatten multiple lists to a single list use the following:

如果您想要将多个列表简单地合并为一个列表,请使用以下方法:

Model.SelectMany(x =&gt; x.SiteNames).ToList() remember to add @using System.Linq to the top of your view.

Model.SelectMany(x =&gt; x.SiteNames).ToList() 记得在视图顶部添加 @using System.Linq

Once you've got this, you can do the following on your Razor page:

一旦您完成了这一步,您可以在 Razor 页面上执行以下操作:

@Html.DropDownListFor(m =&gt; m.SiteId, Model.SelectMany(x =&gt; x.SiteNames).ToList(), &quot;Placeholder Text goes here.&quot;)

英文:

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&lt;SiteListViewModel&gt; as your model, but the DropDown component requires you to have a list of List&lt;SelectListItem&gt;...

If you want to simply flatten multiple lists to a single list use the following:

Model.SelectMany(x =&gt; 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 =&gt; m.SiteId, Model.SelectMany(x =&gt; x.SiteNames).ToList(), &quot;Placeholder Text goes here.&quot;)

答案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&lt;SelectListItem&gt; names = _db.Sites.Select(n =&gt; new SelectListItem { Text = n.SiteId.ToString(), Value = n.Name }).ToList();

View

@Html.DropDownList(&quot;All&quot;, (IEnumerable&lt;SelectListItem&gt;)ViewBag.All) 

huangapple
  • 本文由 发表于 2020年1月3日 21:56:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定