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'.