I have been struggling with a loading a display list of items from a database table that was created through the entity framework.
我一直在努力从通过实体框架创建的数据库表中加载项目的显示列表。
I have found code examples online but many have a static list of items created and then added to the dropdown list list like. http://www.aspnetmvcninja.com/general/asp-net-mvc-dropdown-list-example. I found an article by Scott Allen that is the basis for what I have (http://odetocode.com/Blogs/scott/archive/2010/01/18/drop-down-lists-and-asp-net-mvc.aspx) but I think that issue is that Im not correctly loading the data into _jobs?
我在网上找到了代码示例,但很多都有一个静态的项目列表,然后添加到下拉列表列表中。 http://www.aspnetmvcninja.com/general/asp-net-mvc-dropdown-list-example。我发现Scott Allen撰写的一篇文章是我的基础(http://odetocode.com/Blogs/scott/archive/2010/01/18/drop-down-lists-and-asp-net-mvc。 aspx)但我认为问题是我没有正确地将数据加载到_jobs中?
Here is the code that I have in my controller and other classes, any help or guidance is greatly appreciated as I feel that Im so close and yet not.
这是我在我的控制器和其他课程中的代码,我非常感谢任何帮助或指导,因为我觉得我非常接近而且没有。
public class JobsController : Controller
{
var tasks = db.JobTypes.Select(c => new SelectListItem {Value = c.JobTypeID.ToString(), Text = c.JobDescription});
ViewBag.JobTypeAllNames = tasks;
return View();
}
I have two classs that are being used for my entity framework code first. The Job class references the JobTypes class.
我有两个类首先用于我的实体框架代码。 Job类引用JobTypes类。
public class Job
{
[Key]
[Display(Name="Job ID")]
public int JobID { get; set; }
[Display(Name = "Job Type")]
public int JobTypesID
{
get;
set;
}
private readonly List<JobType> _jobs;
public IEnumerable<SelectListItem> JobTypeItems
{
get
{
return new SelectList(_jobs, "JobTypesID", "JobDescription",1);
}
}
}
public class JobType
{
public int JobTypeID { get; set; }
[Display(Name = "Task")]
public string JobDescription { get; set; }
}
In my view I have
在我看来,我有
@Html.DropDownListFor(model => model.JobTypesID, Model.JobTypeItems);
1 个解决方案
#1
0
This is not going to compile:
这不会编译:
public class JobsController : Controller
{
var tasks = db.JobTypes.Select(c => new SelectListItem {Value = c.JobTypeID.ToString(), Text = c.JobDescription});
ViewBag.JobTypeAllNames = tasks;
return View();
}
You need to have an action that will return an ActionResult
and I would use a model like you have within your view rather than use the ViewBag
:
你需要有一个将返回ActionResult的动作,我会在你的视图中使用你所拥有的模型,而不是使用ViewBag:
public class JobsController : Controller
{
public ActionResult Index()
{
var model = new Job();
var tasks = db.JobTypes.Select(c => new SelectListItem {Value = c.JobTypeID.ToString(), Text = c.JobDescription});
model.JobTypeItems = tasks;
return View(model);
}
}
ps You will need to adapt your Job
model to include a setter and remove un-used _jobs
:
ps您需要调整您的作业模型以包含一个setter并删除未使用的_jobs:
public class Job
{
[Key]
[Display(Name="Job ID")]
public int JobID { get; set; }
[Display(Name = "Job Type")]
public int JobTypesID
{
get;
set;
}
public IEnumerable<SelectListItem> JobTypeItems
{
get; set;
}
}
#1
0
This is not going to compile:
这不会编译:
public class JobsController : Controller
{
var tasks = db.JobTypes.Select(c => new SelectListItem {Value = c.JobTypeID.ToString(), Text = c.JobDescription});
ViewBag.JobTypeAllNames = tasks;
return View();
}
You need to have an action that will return an ActionResult
and I would use a model like you have within your view rather than use the ViewBag
:
你需要有一个将返回ActionResult的动作,我会在你的视图中使用你所拥有的模型,而不是使用ViewBag:
public class JobsController : Controller
{
public ActionResult Index()
{
var model = new Job();
var tasks = db.JobTypes.Select(c => new SelectListItem {Value = c.JobTypeID.ToString(), Text = c.JobDescription});
model.JobTypeItems = tasks;
return View(model);
}
}
ps You will need to adapt your Job
model to include a setter and remove un-used _jobs
:
ps您需要调整您的作业模型以包含一个setter并删除未使用的_jobs:
public class Job
{
[Key]
[Display(Name="Job ID")]
public int JobID { get; set; }
[Display(Name = "Job Type")]
public int JobTypesID
{
get;
set;
}
public IEnumerable<SelectListItem> JobTypeItems
{
get; set;
}
}