ASP.NET Core MVC模型属性绑定为null

编程入门 行业动态 更新时间:2024-10-15 16:18:25
本文介绍了ASP.NET Core MVC模型属性绑定为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

本文发布于:2023-11-14 20:18:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1588425.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   模型   NET   ASP   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!