2

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:

post request data

Debugging screenshot of the posted car

debugging screenshot

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

4
  • 1
    Could you show us the Request ? Commented Jun 9, 2016 at 14:00
  • Despite ASP.NET 5, this may help lbadri.wordpress.com/2014/11/23/… Commented Jun 9, 2016 at 14:09
  • Added a screenshot with the post request data. Commented Jun 9, 2016 at 14:46
  • You're submitting an ID and expecting it to populate, that's not how ASP.NET Binding works or has ever worked? If you want the whole model, you have to submit the whole model. Otherwise post back an ID and load the model server side. Commented Jun 9, 2016 at 14:54

2 Answers 2

3

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the detailed information! Works like a charm.
So when using a ViewModel, we cannot use: [Bind("Color,Name,YearOfConstruction")] with parameter in Controller save function?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.