C# EF MVC4下拉列表中所需的atributte失败
我正在用EF创建一个MVC4应用程序,我有两个简单的类,Brand和CarModel,Brand有一个名称(必选),CarModel有一个品牌(必选)。问题是,我在ModelState.IsValid上遇到了一个错误,因为通过下拉选择品牌,它只会填充品牌ID 我如何在品牌名称中保留所需内容,但能够使用CarModel中的下拉列表?这是我的密码:C# EF MVC4下拉列表中所需的atributte失败,c#,asp.net-mvc,entity-framework,razor,asp.net-mvc-4,C#,Asp.net Mvc,Entity Framework,Razor,Asp.net Mvc 4,我正在用EF创建一个MVC4应用程序,我有两个简单的类,Brand和CarModel,Brand有一个名称(必选),CarModel有一个品牌(必选)。问题是,我在ModelState.IsValid上遇到了一个错误,因为通过下拉选择品牌,它只会填充品牌ID 我如何在品牌名称中保留所需内容,但能够使用CarModel中的下拉列表?这是我的密码: public class Brand { public int Id { get; set; }
public class Brand
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public string Logo { get; set; }
}
public class CarModel
{
public int Id { get; set; }
[Required]
public string Name{ get; set; }
[Required]
public Brand Brand{ get; set; }
}
我有品牌运作的全部积垢。但我正准备为CarModel做这件事,以下是我在create Brand控制器中的内容:
public ActionResult Create()
{
ViewBag.BrandSelect = new SelectList(myRepository.GetBrands.ToList(), "Id", "Name");
return View();
}
[HttpPost]
public ActionResult Crear(CarModel carModel )
{
if (ModelState.IsValid)
{
myrepository.Create(Modelo);
return RedirectToAction("Index");
}
return View(carModel );
}
在我看来
<div class="editor-label">
@Html.LabelFor(model => model.Brand)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Brand.Id,ViewBag.BrandSelect as SelectList)
@Html.ValidationMessageFor(model => model.Brand)
</div>
@LabelFor(model=>model.Brand)
@Html.DropDownListFor(model=>model.Brand.Id,ViewBag.BrandSelect作为SelectList)
@Html.ValidationMessageFor(model=>model.Brand)
如果我理解正确,您的CarModel类应该有一个BrandId(int)和一个Brand对象
public class CarModel
{
public int Id { get; set; }
[Required]
public string Name{ get; set; }
[Required]
public int BrandId { get; set; }
public Brand Brand{ get; set; }
}
您的DropDownList应该绑定到BrandId
@Html.DropDownListFor(model => model.BrandId, ViewBag.BrandSelect as SelectList)
@Html.ValidationMessageFor(model => model.Brand)
您在
BrandSelect
中有一个预先填充的项目,这意味着Brand.Id
保证存在于数据库中。此外,当您创建CarModel
时,您并不是在创建品牌。因此,您可以使用一个表示CarModel
的视图模型,并将其返回到视图,这是最佳实践。考虑这一点:
您的型号
public class CarFormModel // this is a "model class" and not your entity
{
public int Id { get; set; }
[Required]
public string Name{ get; set; }
[Required]
public int BrandId { get; set; }
//replacement for your ViewBag.BrandSelect
//you can also create a viewmodel for this
//but let's use your entity for a more simple example
public IEnumerable<Brand> Brands {get;set;}
}
你的观点
@model CarFormModel
<div class="editor-field">
@Html.TextboxFor(model => model.Name) // capture the name of the car model
<div>
<div class="editor-field">
@Html.DropDownListFor(model => model.BrandId, new SelectList(Model.Brands, "Id", "Name", Model.BrandId)) // capture the brand for the car model
@Html.ValidationMessageFor(model => model.BrandId)
</div>
此选项比现在的更好,因为您没有在下拉列表中使用ViewBag,也没有在视图上公开代理对象。一个好的做法是始终在视图上使用视图模型 谢谢,我知道我应该这样做。谢谢,我试过了,现在已经足够了。如果我错了,请纠正我,但是我认为BrandId
上的[必需的]
属性没有任何区别。由于BrandId
是int
,是一种不可为空的类型,因此它总是有一个值,因此将其设为必需是多余的。要使其可选,您需要使用int?
。我很想知道组合框的验证消息是否显示您已经进行了此更改。从技术上讲,这是正确的。不可为空的字段将按要求处理。更好的形式是拥有它,但不是必需的。
@model CarFormModel
<div class="editor-field">
@Html.TextboxFor(model => model.Name) // capture the name of the car model
<div>
<div class="editor-field">
@Html.DropDownListFor(model => model.BrandId, new SelectList(Model.Brands, "Id", "Name", Model.BrandId)) // capture the brand for the car model
@Html.ValidationMessageFor(model => model.BrandId)
</div>
[HttpPost]
public ActionResult Create(CarFormModel carModel)
{
if (ModelState.IsValid) {
// map the model back to entity or pass it to your service that creates the model
myrepository.Create(the_mapped_or_created_carmodel_entity);
return RedirectToAction("Index");
}
return View(carModel);
}