Model Binding not working

2020-05-27 10:58发布

I have the following POCO classes:

public class Location
    {
        public int LocationId { get; set; }
        public string Name { get; set; }
        public string Street { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string ZipCode { get; set; }
        public string Country { get; set; }
        public float? Latitude { get; set; }
        public float? Longitude { get; set; }
        public string PhoneNumber { get; set; }
        public string EmailAddress { get; set; }
        public string Website { get; set; }
        public virtual ICollection<Program> Programs { get; set; }
        public virtual ICollection<PlayerType> PlayerTypes { get; set; }
    }

public class PlayerType
    {
        public int PlayerTypeId { get; set; }
        public string Name { get; set; }
        public int SortOrder { get; set; }
        public bool IsActive { get; set; }
        public virtual ICollection<Location> Locations { get; set; }
    }

And a View Model Class

public class LocationViewModel
    {
        public Location Location { get; set; }
        public IList<PlayerType> SelectPlayerTypes { get; set; } 

        public LocationViewModel()
        {
            Location = new Location();
        }

    }

Within my Create Form, I have defined the model as

@model Locator.Models.LocationViewModel

And have fields like the following:

div class="editor-label">
    @Html.LabelFor(model => model.Location.Name)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Location.Name)
    @Html.ValidationMessageFor(model => model.Location.Name)
</div>

In my controller to handle the POST I have

[HttpPost]
        public ActionResult Create(LocationViewModel location)
        {
            if (ModelState.IsValid) {
                locationRepository.InsertOrUpdate(location.Location);
                locationRepository.Save();
                return RedirectToAction("Index");
            }
            location.SelectPlayerTypes = golferTypeRepository.All.Where(p => p.IsActive).ToList();
            return View(location);
        }

The problem is that I have a Location object but none of the properties are to set to the values entered in the form.

Am I doing something wrong here?

Thanks

10条回答
看我几分像从前
2楼-- · 2020-05-27 11:16

For anyone else still experiencing an issues with this, if you name your parameter in your post method to be the same as one of your properties, the default modelbinder will also fail.

查看更多
The star\"
3楼-- · 2020-05-27 11:18

This saved my day. Iad the following:

public ActionResult Edit(int masterId, ConclusionView conclusion)

ConclusionView has a property named Conclusion so I nearly lost my mind.

查看更多
虎瘦雄心在
4楼-- · 2020-05-27 11:20

Mine was a bit unique case, but hope it helps someone in similar context. I had my View Model implementing IValidatableObject, and the Validate method from this interface was returning a null if success. It had to return an empty IEnumerable. Hence the binding was actually happening but crashing.

Basically when you get this issue, use Fiddler to look at posted data, make sure the posted variables match the properties(not fields!) on your view models, and put a break point on the View Model constructor to ensure the Routing Engine hits the correct POST method. Good luck!

查看更多
叛逆
5楼-- · 2020-05-27 11:24

Also late to join the party, but after 3 years of asp.net mvc, I've came across that disabled inputs are not posted, so the model binder cannot bind them of course. See here:

"Disabled elements in a form will not be submitted".

Better use readonly="readonly" over disabled. See here.

查看更多
看我几分像从前
6楼-- · 2020-05-27 11:26

Just to contribute another possible reason: I've spent couple of hours looking for an error in my code and the reason for the binder not working in my case was using a model with public fields rather than public properties:

public int CustomerName;

and it should be:

public int CustomerName { get; set; }

Been working on ASP.NET MVC for 3 years now and I never came across this before. Hope it saves somebody some frustration ;)

查看更多
狗以群分
7楼-- · 2020-05-27 11:27

I know it's too late to comment on this.What I did is added parameter less constructor in the ViewModel and everything worked awesome.

查看更多
登录 后发表回答