Pol*_*ial 7 asp.net-mvc asp.net-mvc-5
首先,我是MVC和ASP.NET的新手,如果我遗漏了一些简单的东西,请道歉.
我正在编写一个代码第一个MVC 5应用程序,为了简洁起见,我可以说我有两个这样定义的模型:
public class Platform
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage = "You must select a manufacturer")]
[DisplayName("Manufacturer")]
public int ManufacturerId { get; set; }
public virtual Manufacturer Manufacturer { get; set; }
[Required(ErrorMessage="You must enter a name for the platform")]
[StringLength(50, ErrorMessage="Reduce length to 50 characters or less")]
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和
public class Manufacturer
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage="You must enter a name for the manufacturer")]
[StringLength(50, ErrorMessage="Reduce length to 50 characters or less")]
public string Name { get; set; }
public virtual ICollection<Platform> Platforms { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在创建具有视图的MVC 5控制器时,使用Entity Framework对这些模型的脚手架,所有CRUD操作都能正常工作.另一方面,表单元素未格式化,缺少form-controlBootstrap想要的类.
我可以通过添加一个类似于@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @class = "form-control" })它的EditorTemplate来解决每种类型的问题,但是Bootstrap MVC 5项目的脚手架肯定应该发出有效的代码吗?看看各种MVC 5教程,他们似乎正确地使用了正确的样式,所以我对我做错了什么感到困惑.
作为参考,每个模型的Create.cshtml文件中的输出(为简洁而剪切到重要的表单元素)是:
<div class="form-group">
@Html.LabelFor(model => model.ManufacturerId, "ManufacturerId", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ManufacturerId", String.Empty)
@Html.ValidationMessageFor(model => model.ManufacturerId)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
和
<div class="form-group">
@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
为什么在模型中设置ManufacturerIda时将被指定为下拉列表的标签文本DisplayName?
为什么下拉使用DropDownList()而不是强类型DropDownListFor()?
先感谢您.
听起来像你做的那样 - 将Bootstrap 2.x升级到3.0或3.0.1.Microsoft项目模板基于Bootstrap 2.x而不是3.x. 你可以在GitHub上阅读其他人 .
我也得到了微软的Rick Anderson的回复,他在www.asp.net上写了几个教程,他证实了这一点......
很棒的.实际上,我的教程中的一些图像来自Beta和RC,它们使用的是bootstrap v2,而不是V3,所以我们的图像会有所不同.查看帐户控制器的视图以查看"表单控件".您可以下载我的完整项目并与您的项目进行比较.
关于你的另一个问题,我遇到了同样的问题,并认为这可能是一个错误.某些注释似乎被忽略了.尝试将[ForeignKey()]注释添加到Platform.ManufacturerId并查看它是否影响字段标签.
| 归档时间: |
|
| 查看次数: |
5375 次 |
| 最近记录: |