简体   繁体   中英

ASP.NET Core MVC model property binding is null

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.

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

The main model Car

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

The Cars controller

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

The request data:

发布请求数据

Debugging screenshot of the posted car

调试截图

Can u give me a helping hand, how the property could be "bound" and so the ModelState.IsValid == true ?

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)

It means: name of your input/select on the View must be "Color.CarColorId".

... 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.

correct way

domain models (without changes):

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

View model:

public class CarModel
{    
    [MinLength(2)]
    public string Name { get; set; }

    [Required]
    public DateTime YearOfConstruction { get; set; }

    [Required]
    public int ColorId { get; set; }    
}

Controller:

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

In my case I had auto generated internal set properties on my model class. Framework couldn't bind those. Maybe this post will help someone in future as it took me a while to figure this out ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM