When i try to load the page with my DropDownList i get an error "
The model item passed into the dictionary is of type 'System.Web.Mvc.SelectList', but this dictionary requires a model item of type 'FBChecklist.Server'
. In the autogenerated model (Server.cs) i
manually added the SelectListItem as below :
<pre>public partial class Application
{
public int ServerId { get; set; }
public string ServerIp { get; set; }
public Nullable<int> ApplicationId { get; set; }
public IEnumerable<SelectListItem> Applications { get; set; }
}
This is my default table structure in SqlServer :
Server table
int ServerId
nvarchar(50) ServerIp
int ApplicationId
and the DropDownList binds from the table below :
Application
int ApplicationId
nvarchar(50) ApplicationName.
and the generated model :
public partial class Application
{
public int ApplicationId { get; set; }
public string ApplicationName { get; set; }
}
In the repository(ServersService.cs) i'm retrieving the data as below :
<pre> public IEnumerable<SelectListItem> GetApplications()
{
using (var db = new AppEntities())
{
List<SelectListItem> applications = db.Applications.AsNoTracking()
.OrderBy(n => n.ApplicationName)
.Select(n =>
new SelectListItem
{
Value = n.ApplicationId.ToString(),
Text = n.ApplicationName
}).ToList();
var apptip = new SelectListItem()
{
Value = null,
Text = "--- select application ---"
};
applications.Insert(0, apptip);
return new SelectList(applications, "Value", "Text");
}
}
And then in the Controller i have wired up as :
public ActionResult Create()
{
var apps = serversService.GetApplications();
return View(apps);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ServerIp,ServerName,CreatedBy,CreatedOn")] Server server)
{
if (ModelState.IsValid)
{
serversService.AddServer(server); ;
return RedirectToAction("Index");
}
return View(server);
}
What I have tried:
For now it appears from my research there is no direct way to add properties such as SelectListItem to the autogenerated model in Database First. Any assistance on how i can achieve this using Database First approach or at least refactor the way i have implemented my code. The only thing i have seen yet that can be added to this partial class is Data Annotations.