【发布时间】:2016-06-09 13:54:47
【问题描述】:
我正在使用带有实体框架的 ASP.NET Core RC2 MVC 并尝试保存一辆新车。问题是,在汽车控制器的 create 方法中,当动作被回发时,属性 Color 为 null。设置所有其他属性/字段。但是引用 CarColors 模型的 Color 为空。
CarColor 模型
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);
}
请求数据:
贴车调试截图
你能帮我一把吗,如何“绑定”属性以及 ModelState.IsValid == true?
【问题讨论】:
-
你能告诉我们请求吗?
-
尽管有 ASP.NET 5,这可能会有所帮助 lbadri.wordpress.com/2014/11/23/…
-
添加了发布请求数据的屏幕截图。
-
您正在提交一个 ID 并期望它填充,这不是 ASP.NET 绑定的工作方式或曾经工作过吗?如果你想要整个模型,你必须提交整个模型。否则回传一个 ID 并加载模型服务器端。
标签: c# asp.net-core asp.net-core-mvc