3

I'm facing a really strange problem that has me smoked.

I have a fairly simple scenario where I have a strongly typed view that is correctly populated from the controller on the GET, but then when it POSTS the form to the controller, the Reqeust is full of all the right values and right key names for the default model binder to correctly populate one of my model objects, and the DMB creates the correct opject, but it never populates any of the properties, they're all in their default state.

This was working before, the only changes I can think of were that I tried a custom modelbinder (then removed it; double checked to make sure I'm not still using that), and I refactored the model to have a base class with some of the props.

Any thoughts?

tereško
  • 58,060
  • 25
  • 98
  • 150
Paul
  • 35,689
  • 11
  • 93
  • 122
  • If it was working before, you could try roll back to that point & then start making your changes again one at a time & work out what's breaking it... – Alconja Jun 05 '09 at 06:15
  • Post the code =) We are not mind readers. – zihotki Jun 05 '09 at 10:15
  • Sorry, I didn't think the code was all that interesting. [AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(Person p) { /* right here, p is initialized with defaults only, nothing from the form; Request has all the right stuff in it. */ – Paul Jun 06 '09 at 13:03
  • This resolved the problem in my case: [http://stackoverflow.com/questions/17858852/default-model-binder-not-working](http://stackoverflow.com/questions/17858852/default-model-binder-not-working) – Rahatur Oct 06 '15 at 08:58

4 Answers4

3

A very similar scenario - that the DefaultModelBinder is - essentially - not binding to your model, arrise if you would give your bound model object the same name as one of its properties:

Model

Public Property ArbitraryName1 As Integer
Public Property Answer As String
Public Property ArbitraryName2 As Boolean

View

<p/> ... @Model.ArbitraryName1
<p/> Answer: @Html.TextBoxFor(Function(model) model.Answer)
<p/> ... @Html.CheckBoxFor(Function(model) model.ArbitraryName2)

Controller

<HttpPost()>
Function Index(answer As Model) As ActionResult
  ' answer is Nothing
End Function

(Using ASP.NET MVC 3)

Sami Lamti
  • 215
  • 2
  • 10
0

The name of your input param do not have to be equal to some property name of the object. Remember that all data coming as an array of name -> value and the default binding use the names for make the relation work.

user2539146
  • 51
  • 1
  • 2
0

I had this behaviour arise by moving two properties from the top of the class to further down. I still can't work out why this stopped the binding of a third property from working (so this isn't a solution so much as a 'watch out for') but I repeated the change multiple times and each time the binding went from working to not working. I also found that after making this change I sometimes had to 'Clean' the solution for the binding to start working again.

christutty
  • 952
  • 5
  • 12
0

Got it. The model had been refactored in a way which naturally affected the ability of the mdoel binder to populate it.

Paul
  • 35,689
  • 11
  • 93
  • 122
  • 1
    How did you refactor it and what did you do to make it work? I can't tell if my problem is similar to yours. – Tilendor Mar 03 '10 at 21:59
  • sorry for being mysterious. Basically, I had been pushing a Person object to the view in the GET action and then getting a Person object from the form in the POST action. I changed the GET action to populate the form w/ a viewmodel that had more information, and the viewmodel had a Person as one of its properties, which changed how the names were generated in the form fields. Changing the POST to use a more similar structure fixed the problem. – Paul Jul 24 '10 at 12:32
  • I've got a similar problem - what do you mean 'changing the post'? – codeulike Sep 23 '10 at 18:26
  • I meant the names of the form fields. – Paul Sep 24 '10 at 05:04