ASP.NET Core MVC模型属性绑定为null [英] ASP.NET Core MVC model property binding is null
问题描述
我将ASP.NET Core RC2 MVC与Entity Framework结合使用,并试图保存一辆新车.问题是,在回传动作后,在汽车控制器的create方法中,属性 Color 为 null .设置所有其他属性/字段.但是引用 CarColors 模型的Color为空.
I am using ASP.NET Core RC2 MVC with Entity Framework and trying to save a new car. The problem is, that in the create method of the car controller the property Color is null when the action is posted back. All other properties/fields are set. But the Color which refers to the CarColors model is null.
CarColor模型
The CarColor model
public class CarColor
{
[Key]
public int CarColorId { get; set; }
[MinLength(3)]
public string Name { get; set; }
[Required]
public string ColorCode { get; set; }
}
主要模型车
public class Car
{
[Key]
public int CarId { get; set; }
[MinLength(2)]
public string Name { get; set; }
[Required]
public DateTime YearOfConstruction { get; set; }
[Required]
public CarColor Color { get; set; }
}
汽车控制器
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Color,Name,YearOfConstruction")] Car car)
{
if (ModelState.IsValid)
{
_context.Add(car);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(car);
}
请求数据:
已发布汽车的调试屏幕截图
Debugging screenshot of the posted car
您能帮我一下,如何绑定属性吗? ModelState.IsValid == true 吗?
Can u give me a helping hand, how the property could be "bound" and so the ModelState.IsValid == true?
推荐答案
如果要填充Car模型的Color属性,则您的请求必须类似于:
If you want populate Color property of your Car model, your request must looks like:
[0] {[Name, Volvo]}
[1] {[Yearof Construction, 19/16/2015]}
[2] {[Color.CarColorId, 3]} (will be "bound" only ID)
这意味着:您在视图"上输入/选择的名称必须为"Color.CarColorId".
It means: name of your input/select on the View must be "Color.CarColorId".
...但是您选择了错误的方式.您不应在视图中直接使用域模型.您应该为View和操作方法的传入属性创建专用的View模型.
... But you chose incorrect way. You should not use Domain models in your View directly. You should create View Models special for View and for incoming properties of you action methods.
正确方法
域模型(无更改):
public class CarColor
{
[Key]
public int CarColorId { get; set; }
[MinLength(3)]
public string Name { get; set; }
[Required]
public string ColorCode { get; set; }
}
public class Car
{
[Key]
public int CarId { get; set; }
[MinLength(2)]
public string Name { get; set; }
[Required]
public DateTime YearOfConstruction { get; set; }
[Required]
public CarColor Color { get; set; }
}
查看模型:
public class CarModel
{
[MinLength(2)]
public string Name { get; set; }
[Required]
public DateTime YearOfConstruction { get; set; }
[Required]
public int ColorId { get; set; }
}
控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(CarModel model)
{
if (ModelState.IsValid)
{
var color = await _context.Colors.FirstAsync(c => c.CarColorId == model.ColorId, this.HttpContext.RequestAborted);
var car = new Car();
car.Name = model.Name;
car.YearOfConstruction = model.YearOfConstruction;
car.Color = color;
_context.Cars.Add(car);
await _context.SaveChangesAsync(this.HttpContext.RequestAborted);
return RedirectToAction("Index");
}
return View(car);
}
这篇关于ASP.NET Core MVC模型属性绑定为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!