MVC 4简单填充下拉从数据库模型 [英] MVC 4 Simple Populate DropDown from database model
问题描述
我觉得自己有点笨。
我试图让MVC 4的窍门,用拳击作为一种功能性的例子。
I'm trying to get a hang of MVC 4, using boxing as a functional example.
我 WeightCategories
在数据库中(重量级
等)和义和团
。
I have WeightCategories
in the database (Heavyweights
, etc), and Boxers
.
看起来似乎很简单。的关系是一个拳击手有一个当前体重级别,但是当我编辑,我希望它能够与一个下拉去改变它。
Seem simple. The relation is a boxer has a current weight category, but when I edit, I want it to be able to change it with a drop down.
我知道如何做到这一点,如果是我做了自己在code名单,但我有问题,理解如何装入从 WeightCategory $ C列表$ C>表并显示在拳击手的看法/模式。
I understand how to do it if it's a list I've made myself in the code, but I have problem understanding how to "load" the list from the WeightCategory
table and show it in the view/model of the boxer.
所以,这里是我的code为 WeightCategory
项目:
So, here is my code for the WeightCategory
item:
[Table("WeightCategories")]
public class WeightCategory
{
[Key]
public int WeightCategoryId { get; set; }
public WEIGHT_CATEGORIES WeightCategoryType { get; set; }
[Display(Name = "Weight Category Name")]
[Required]
[MinLength(5)]
public string Name { get; set; }
[Display(Name = "Weight Limit In Pounds")]
public int? WeightLimit { get; set; }
}
下面是code为拳击项目
Here is the code for the boxer item
[Table("Boxers")]
public class Boxer
{
[Key]
public int BoxerId { get; set; }
public WeightCategory CurrentWeightCategory { get; set; }
[Required]
public string Name { get; set; }
public int Wins { get; set; }
public int Losses { get; set; }
public int Draws { get; set; }
public int Kayos { get; set; }
}
在看,我真的不知道如何解决的是,我在pretty确保它不是自动的,我需要的地方装载表控制器,也许......我在寻找最好的实践什么的。
In the view, I'm really not sure how to tackle that, I'm pretty sure it's not automatic and I need to load the table somewhere in the controller maybe... I'm looking for best practice or something.
类似的东西在视图中底:
Something like that in the view at the end:
@Html.DropDownListFor(model => model.CurrentWeightCategory.WeightCategoryId,
new SelectList(Model.WeightCategories, "WeightCategoryId", "Name",
Model.WeightCategories.First().WeightCategoryId))
谢谢!
推荐答案
您可以设计一个视图模型:
You could design a view model:
public class MyViewModel
{
public Boxer Boxer { get; set; }
public IEnumerable<SelectListItem> WeightCategories { get; set; }
}
再有你的控制器动作填充,并通过该视图模型到视图:
and then have your controller action populate and pass this view model to the view:
public ActionResult Edit(int id)
{
var model = new MyViewModel();
using (var db = new SomeDataContext())
{
// Get the boxer you would like to edit from the database
model.Boxer = db.Boxers.Single(x => x.BoxerId == id);
// Here you are selecting all the available weight categroies
// from the database and projecting them to the IEnumerable<SelectListItem>
model.WeightCategories = db.WeightCategories.ToList().Select(x => new SelectListItem
{
Value = x.WeightCategoryId.ToString(),
Text = x.Name
})
}
return View(model);
}
现在你的看法变成强类型的视图模型:
and now your view becomes strongly typed to the view model:
@model MyViewModel
@Html.DropDownListFor(
x => model.Boxer.CurrentWeightCategory.WeightCategoryId,
Model.WeightCategories
)
这篇关于MVC 4简单填充下拉从数据库模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!