Model Binding not working

后端 未结 10 1434
别那么骄傲
别那么骄傲 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:03

    And another reason: Normally I use the built in editors or displays for and would have never encountered this issue. However in this case I required a semi-custom control. Basically a drop down with lots of data attributes on the options.

    What I was doing, for my select tag was:

    <select name="@Html.IdFor(x => x.SubModel.ID)" id="@Html.IdFor(x => x.SubModel)" class="form-control select-control">
    

    Now everything was posting back and to the untrained eye it looked like everything should work and bind. However the IdFor helper renders sub models with an underscore. The model binder doesn't interpret underscores as a class hierarchy indicator. What should be separating them is a dot. Which comes from NameFor:

    <select name="@Html.NameFor(x => x.SubModel.ID)" id="@Html.IdFor(x => x.SubModel)" class="form-control select-control">
    

    NameFor fixed all my issues.

    0 讨论(0)
  • 2021-02-06 02:07

    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!

    0 讨论(0)
  • 2021-02-06 02:12

    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.

    0 讨论(0)
  • 2021-02-06 02:13

    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.

    0 讨论(0)
  • 2021-02-06 02:13

    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.

    0 讨论(0)
  • 2021-02-06 02:14

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

    0 讨论(0)
提交回复
热议问题