Null or empty model values after posting to controller and editing model

Null or empty model values after posting to controller and editing model

I have been working with MVC for a little while now and always learning new things about it, yesterday I ran into something for the very first time, so wanted to share.

I’ve seen the pattern of using a single view for both creation/editing of a model, it’s not very uncommon. In this particular case, I was working with a controller that would save its bound model to the database and then assign it the unique id generated in the database. The idea being that if the id was null, this was a new model, and if the id existed, this was a model to be edited. After this id was assigned the controller returned the same view with the updated model. The only problem was that this newly edited unique id for the model was never making it back to the view. This was verified by a consistently empty @Html.HiddenFor input for the models id property.

So here is what I learned… when you are posting a model to a controller action, and then changing some values on that model, and returning the same view with that updated model…MVC will not use those model values. Instead, it will use the ModelState dictionary to populate our view.

There are a few ways around this (one of them being clearing model state in your controller), and thanks to Tejasvi’s explanation on this SOF thread, I now feel like I understand this more. Although, I am not entirely certain I understand why it works like this. That’s for another post 🙂

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