- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我觉得有点傻。
我试图掌握 MVC 4 的窍门,使用装箱作为功能示例。
我在数据库中有 WeightCategories
(Heavyweights
等)和 Boxers
。
看似简单。关系是一个拳击手有一个当前的体重类别,但当我编辑时,我希望它能够通过下拉菜单改变它。
如果它是我自己在代码中制作的列表,我知道该怎么做,但我无法理解如何从 WeightCategory
表中“加载”列表并将其显示在拳击手的 View /模型。
所以,这是我为 WeightCategory
项目编写的代码:
[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; }
}
这是拳击手项目的代码
[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; }
}
在 View 中,我真的不确定如何解决这个问题,我很确定它不是自动的,我可能需要将表加载到 Controller 中的某个位置......我正在寻找最佳实践或其他东西.
最后的 View 中类似的东西:
@Html.DropDownListFor(model => model.CurrentWeightCategory.WeightCategoryId,
new SelectList(Model.WeightCategories, "WeightCategoryId", "Name",
Model.WeightCategories.First().WeightCategoryId))
最佳答案
你可以设计一个 View 模型:
public class MyViewModel
{
public Boxer Boxer { get; set; }
public IEnumerable<SelectListItem> WeightCategories { get; set; }
}
然后让您的 Controller 操作填充并将此 View 模型传递给 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);
}
现在你的 View 变成了 View 模型的强类型:
@model MyViewModel
@Html.DropDownListFor(
x => model.Boxer.CurrentWeightCategory.WeightCategoryId,
Model.WeightCategories
)
关于c# - MVC 4 Simple Populate DropDown 从数据库模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14304331/
我正在尝试让 Bootstrap 下拉菜单与我的 Jade 布局一起使用。但实际上,当我尝试在我的 Jade 文件中执行 $(".dropdown-toggle").dropdown 时出现此错误:
我正在使用 Semantic React UI's Dropdown ,并且我希望它在其父组件安装后立即获得焦点:用户应该能够立即搜索。 我尝试在父级的 render() 中使用 ref: (thi
我有一个脚本如下: var i = 0; function add_relation() { i = i + 1; var key = $('', {
我在 MVC 中有 2 个 DropDowns 并尝试使用 AngularJS。我的第一个 DropDown 填充了正确来自数据库的数据。当用户在第一个 DropDown 上进行选择时,第二个 Dro
我是一名开发新手,但我偶然发现了一个我无法解决的案例。我将 ASP.NET MVC 与 C# 以及一些 javascript(JQuery、JSON...)结合使用。 我要做的是根据在 other 中
在 Bootstrap 文档中,似乎有两种不同的方法来创建下拉列表: .dropdown .btn-group first如下: Dropdown ...
我确实在 kv 文件中定义了一个基本的自定义 DropDown。应用程序 GUI 非常简单,顶部有一个按钮栏,屏幕的其余部分有一个 TextInput。这是代码: dropdowntrialgui.p
在移动 View 中单击 .dropdown 菜单时,.dropdown 和 .dropdown-menu 之间有一个小空间. 如图所示 Question is it possible to remo
我正在尝试创建一个类似于 DataEntry 网格/表的结构,其中用户在单击按钮时追加新行。该行将包含文本框、下拉菜单、日历控件。所有这些都应该在客户端完成。到目前为止,我已经有了这个,正如你所看到的
我有一个带有使用 ul 和 li ( jsfiddle ) 的下拉列表的 jsfiddle。尝试将其转换为选择选项下拉列表时,我没有得到相同的选项 ([jsfiddle]) 2 .我猜这与我重命名某个
我正在使用@atlaskit/dropdown-menu (https://atlaskit.atlassian.com/packages/core/dropdown-menu)。我想在 Item 点
我在我的 angular 应用程序中使用 PrimeNG,我有 p-dropdown 的问题 问题 我为 country 和 caste_category 有两个下拉菜单,我只为 India 提供 c
我有一个表单 View ,在编辑模板中我有两个下拉菜单。 下拉 1 明确设置了允许值的列表。它也设置为自动回发。 下拉 2 是数据绑定(bind)到 objectdatasource,这个 objec
我有一个名为 Agency 的下拉菜单,其中包含 2 个选项: 文凭 PPS 如果选择 SPM,则不会出现下拉菜单。但是,如果选择 PPS,则会出现另外 2 个名为 Department 和 Offi
我是 Angular 的新手,所以这可能是一个非常简单的问题。我正在使用 ngx-select-dropdown (下拉控件)。页面上有两个下拉控件,当第一个下拉列表更改时,第二个下拉列表应更新下拉选
我有一个包含文本区域的下拉菜单。当在文本区域按下返回键时,我希望关闭下拉菜单。这是以下内容的作用: $(document).ready(function(){ $('#edit').keypres
我的页面中有这些SELECT Pending Confirmed Cancelled Refunded Shipped Paid Complete 还有这个 Pending Confirme
我有兴趣学习以智能、优雅的方式在整个站点中构建导航项,这些导航项具有胡萝卜,单击后会展开菜单项列表。 我希望动态构建菜单项(不一定全部使用 ajax)。但它们不应在页面上进行硬编码。 这似乎是像 TW
我有一个选择下拉菜单,一个是 Bootstrap 导航下拉菜单,而不是单击以打开 Bootstrap 导航下拉菜单我在鼠标悬停时更改了它但是当选择下拉菜单打开并且鼠标悬停在 Bootstrap 导航上
我对此很陌生,所以如果我的问题没有按应有的方式提出,我深表歉意。 基本上,我使用 jQuery 的目的是,当从第一个下拉框中选择名为“Apple”的字段时,第二个下拉框将只允许选择“Firm”字段,而
我是一名优秀的程序员,十分优秀!