【问题标题】:ASP.NET Core MVC model property binding is nullASP.NET Core MVC 模型属性绑定为空
【发布时间】: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


【解决方案1】:

如果您想填充 Car 模型的 Color 属性,您的请求必须如下所示:

[0] {[Name, Volvo]}
[1] {[Yearof Construction, 19/16/2015]}
[2] {[Color.CarColorId, 3]} (will be "bound" only ID)

这意味着:您在视图上输入/选择的名称必须是“Color.CarColorId”。

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

正确方法

领域模型(未更改):

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);
}

【讨论】:

  • 感谢您提供详细信息!像魅力一样工作。
  • 那么当使用 ViewModel 时,我们不能使用:[Bind("Color,Name,YearOfConstruction")] with parameter in Controller save function?
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 2021-06-27
    • 2017-02-13
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多