Model Binding not working

后端 未结 10 1381
别那么骄傲
别那么骄傲 2021-02-06 01:52

I have the following POCO classes:

public class Location
    {
        public int LocationId { get; set; }
        public string Name { get; set; }
        publi         


        
10条回答
  •  有刺的猬
    2021-02-06 02:21

    Here's the problem:

    [HttpPost]
    public ActionResult Create(LocationViewModel location)
    

    Do you see it? It's the name of your action argument: location.

    Look at your view model now, it has a property named Location:

    public Location Location { get; set; }
    

    This confuses the model binder. It no longer knows whether you need to bind the LocationViewModel or its property.

    So simply rename to avoid the conflict:

    [HttpPost]
    public ActionResult Create(LocationViewModel model)
    

提交回复
热议问题