简体   繁体   English

ASP.NET使用MVC将linq查询结果绑定到HTML.DropDownList()

[英]ASP.NET Binding linq query results to HTML.DropDownList() using MVC

I am trying to build a drop down list with a single database call. 我正在尝试使用单个数据库调用构建一个下拉列表。

My table consists of an Id column and a CompanyName column. 我的表由Id列和CompanyName列组成。 I need to display the CompanyName to the user and when one is selected set the Id for the page to Id. 我需要向用户显示CompanyName,当选择一个时,将页面的Id设置为Id。

I have put a simple model together to store the information: 我把一个简单的模型放在一起来存储信息:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace Portal.Models
{
    public class CompanyListId
    {
        public int Id { get; set; }
        public string CompanyName { get; set; }
    }

    public class CompanyListIdDBContext : DbContext
    {
        public DbSet<CompanyListId> Contacts { get; set; }
    }
}

But I am having an issue with the controller and razor call in the view. 但我在视图中遇到了控制器和剃须刀调用的问题。

My controller: 我的控制器:

public void CompanyListIdSearch()
        {
            using (var dc = new CompanyListIdDBContext())
            {
                var content = from p in db.Companies
                                select new { p.CoId, p.CompanyName };
            }
        }

Razor call in View: Razor在视图中调用:

<div id="partners" class="linen">

                        @{Html.DropDownList("CompanyListIdSearch", new SelectList(Model.CompanyName));}
                    </div>

Grabbing the model at the top of a view @model Portal.Models.CompanyListId 在视图@model Portal.Models.CompanyListId的顶部@model Portal.Models.CompanyListId

So at a high level what I am trying to do is have a view that calls the controller with a razor tag, the controller updates the model with data and then that data is displayed in the drop down. 所以在高级别我想要做的是有一个用剃刀标签调用控制器的视图,控制器用数据更新模型,然后在下拉列表中显示该数据。 Am I approaching this the wrong way? 我是以错误的方式接近这个吗?

I am getting this error: 我收到此错误:

The model item passed into the dictionary is of type 'Portal.Models.DashboardContents', but this dictionary requires a model item of type 'Portal.Models.CompanyListId'. 

Not quite. 不完全的。 You can't call a method in DropDownList like that. 你不能像这样调用DropDownList的方法。

Firstly, put that collection of items in your ViewBag : 首先,将这些项目集合放在ViewBag

using (var dc = new CompanyListIdDBContext())
{
    var content = from p in db.Companies
       select new { p.CoId, p.CompanyName };

    ViewBag.CompaniesList = content
        .Select(c => new SelectListItem
        {
            Text = c.CompanyName,
            Value = c.CoId.ToString()
        }).ToList();
}

Make sure you call that function in your action result before returning the view. 确保在返回视图之前在操作结果中调用该函数。

Lastly, just use your model property and switch to DropDownListFor : 最后,只需使用您的模型属性并切换到DropDownListFor

@Html.DropDownListFor(m => m.Id, ViewBag.CompaniesList);

A better approach however , is to not use the ViewBag and to use a view model instead. 但是更好的方法是不使用ViewBag并使用视图模型。 Something like this: 像这样的东西:

public class CompanyViewModel
{
    public int Id { get; set; }
    public string CompanyName { get; set; }
    public List<SelectListItem> CompaniesList { get; set; }
}

Then your action method would be something like: 然后你的行动方法将是这样的:

public ActionResult YourAction()
{
    var model = new CompanyViewModel();

    using (var dc = new CompanyListIdDBContext())
    {
        var content = from p in db.Companies
            select new { p.CoId, p.CompanyName };

        model.CompaniesList = content
            .Select(c => new SelectListItem
            {
                Text = c.CompanyName,
                Value = c.CoId.ToString()
            }).ToList();
    }

    return View(model);
}

Then your view would use the new model: 然后您的视图将使用新模型:

@model CompanyViewModel

Then your dropdown would be: 然后你的下拉列表是:

@Html.DropDownListFor(m => m.Id, Model.CompaniesList)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JavaScript Asp.Net MVC复制@ Html.DropdownList - Clone @Html.DropdownList using javascript Asp.Net MVC 如何在asp.net mvc中为html.DropDownList设置值? - how to set a value for the html.DropDownList in asp.net mvc? ASP.NET MVC-将JsonConvert.DeserializeObject转换为Html.DropDownList - ASP.NET MVC - JsonConvert.DeserializeObject to Html.DropDownList Asp.Net MVC 5 Html.DropDownList无法验证 - Asp.Net MVC 5 Html.DropDownList is not validating 使用 ASP.NET MVC Razor 在 JQuery 中填充 Html.DropDownList() - Populate Html.DropDownList() in JQuery using ASP.NET MVC Razor 使用C#asp.net MVC代码优先方法在Razor中验证@ Html.DropDownList和@ Html.DropDownListFor - Validation for @Html.DropDownList and @Html.DropDownListFor in Razor using C# asp.net mvc code first approach 在ASP.NET MVC中的html.dropdownlist onchange上填充html.textbox - fill html.textbox upon html.dropdownlist onchange in asp.net mvc @ Html.ListBox()和@ Html.DropDownList()如何在asp.net mvc中保持状态? - How does @Html.ListBox() and @Html.DropDownList() keep the state in asp.net mvc? 在 html.dropdownlist 的 on-change 事件上从视图 MVC Asp.net 调用特定方法(不是操作) - Calling specific method (not an Action) from view MVC Asp.net on on-change event of html.dropdownlist ASP.NET MVC RC(刷新)中的 Html.DropDownList 未预选项目 - Html.DropDownList in ASP.NET MVC RC (refresh) not pre-selecting item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM