C# ASP.NET核心MVC模型属性绑定为空

C# ASP.NET核心MVC模型属性绑定为空,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,我正在使用ASP.NET Core RC2 MVC和实体框架,并试图保存一辆新车。问题是,在汽车控制器的create方法中,当动作被发回时,属性Color为空。设置所有其他属性/字段。但表示CarColors模型的颜色为空 CarColor模型 public class CarColor { [Key] public int CarColorId { get; set; } [MinLength(3)] public string Name { get; set

我正在使用ASP.NET Core RC2 MVC和实体框架,并试图保存一辆新车。问题是,在汽车控制器的create方法中,当动作被发回时,属性Color为空。设置所有其他属性/字段。但表示CarColors模型的颜色为空

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);
}
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务创建([Bind(“颜色、名称、施工年份”)]Car)
{
if(ModelState.IsValid)
{
_上下文。添加(car);
wait_context.SaveChangesAsync();
返回操作(“索引”);
}
返回视图(car);
}
请求数据:

调试张贴的汽车屏幕截图


您能帮我一个忙吗?如何“绑定”属性,从而使ModelState.IsValid==true

如果您想填充您的车型的颜色属性,您的请求必须如下所示:

[0] {[Name, Volvo]}
[1] {[Yearof Construction, 19/16/2015]}
[2] {[Color.CarColorId, 3]} (will be "bound" only ID)
这意味着:视图上输入/选择的名称必须为“Color.CarColorId”

。。。但你选择了错误的方式。您不应该直接在视图中使用域模型。您应该为视图和动作方法的传入属性创建专用的视图模型

正确的方法

域模型(无更改):

视图模型:

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);
}
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务创建(CarModel模型)
{
if(ModelState.IsValid)
{
var color=wait_context.Colors.FirstAsync(c=>c.CarColorId==model.ColorId,this.HttpContext.RequestAborted);
var car=新车();
car.Name=model.Name;
car.YearOfConstruction=model.YearOfConstruction;
颜色=颜色;
_context.Cars.Add(car);
wait_context.SaveChangesAsync(this.HttpContext.RequestAborted);
返回操作(“索引”);
}
返回视图(car);
}

在我的例子中,我在模型类上自动生成了
内部集
属性。框架无法绑定这些。也许这篇文章将来会对某人有所帮助,因为我花了一段时间才弄明白;)

您能给我们看一下请求吗?尽管有ASP.NET 5,但这可能有助于添加一个包含请求后数据的屏幕截图。您提交的是一个ID并希望它填充,而ASP.NET绑定不是这样工作的,也从来没有这样工作过?如果你想要整个模型,你必须提交整个模型。否则,请发回ID并加载模型服务器端。感谢提供详细信息!工作起来很有魅力。所以当使用ViewModel时,我们不能在控制器保存函数中使用参数:
[Bind(“Color,Name,YearOfConstruction”)]
[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);
}