简体   繁体   中英

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. I need to display the CompanyName to the user and when one is selected set the Id for the page to 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:

<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

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.

Firstly, put that collection of items in your 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 :

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

A better approach however , is to not use the ViewBag and to use a view model instead. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM