MVC Model Binding Not Working with Fields

MVC Model Binding Not Working with Fields

This will be a very quick/simple post.

Maybe this is very clear to most people when they start out developing with ASP.NET MVC but it was not right away with me…

When you are using 2-way model binding in MVC to bind your model to your view and back again, you MUST use properties with getters/setters, not fields.

//Will NOT bind ok public class BindingModel {
    int ID;
    string FirstName;
    string LastName;
    string Website;
}

//WILL bind ok public class BindingModel {
    int ID { get; set; }
    string FirstName { get; set; }
    string LastName { get; set; }
    string Website { get; set; }
}

I don’t understand why this is, but I have not really looked into it in too much depth. I just made my variables into properties with auto getter/setters and went on my way for now. Hopefully this will save someone some time the next time they are trying to get MVC model binding to work, and can’t figure out why their variables are not being set on the model.

There is 1 comment .

brent

Thanks for the comment Jorge. That does indeed look to be a similar issue to what I experienced. Thanks for sharing.

Reply »

Share Your Thoughts!

Testimonials

What My Clients Say

Karin - Hope and Future

Brent has always been very prompt and generally exceeds our expectations with his work. His maintenance fee is very reasonable. I strongly recommend him!  We get many compliments on our beautiful web site!

Dan

I contacted Krueger Web Design and Brent did everything.  He setup a test site with the changes so we could verify everything was correct before we transferred from my old hosting provider.  During the setup and conversion Brent was extremely professional and responsive. He responded to any issues the same day.

Dan Kaufman
Emily - GFCLT

We've had a great experience working with Krueger Web Design. Prompt responses, great ideas, effective work... all at a reasonable price! This was the perfect fit for our organization.

Mahmud - ACMC

I would love to recommend you to anyone! You have been very helpful when we needed you the most.

Brian - SEMNIC

Working with Krueger Web Design was effortless. Communicating what I wanted for the website and the results that I saw were identical. The customer service was always prompt and I am completely satisfied with the product.

Zach - Stadium Club

Exactly what I wanted! Fast turnaround time and very affordable. Answered all of our questions quickly and made adjustments promptly. Very easy to work with and puts everything into terminology that even I can understand!

Contact Krueger Web Design

Have a question for me? Need an estimate on some work? You can email me at brent@kruegerwebdesign.com or drop me a line using the button below:

Copyright © 2017 Krueger Web Design - All rights reserved
Web Design in Madison, WI