question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ASP.NET MVC: updated model data is not recognised in view

See original GitHub issue

Hi there,

first off, I’m rather new to ASP.NET and its ecosystem, so bear with me if wording is not on spot. I don’t want to exclude layer 8 as the root cause of the following problem either, but neither the docs nor intensive usage of search engines helped at all.

Describe the bug

I’m using APS.NET MVC technology for the following.

So, I have a very simple example to cover. Imagine a form, it has two text fields and a checkbox. In order to back this form, I created a simple view model class having two string properties and a bool property. One of the string properties is required while the other is optional.

In my controller there is an action that will simply return a view, getting passed a fresh instance of that model. The view will render the form using the <form asp-route>…, <input asp-for="..."> tags. So far, so good, everything works until now.

Then I have another controller action that is the target of the form. It will check for model validity using the ModelState.IsValid property. If the model is not valid, I want to reset the optional field and unset the checkbox (setting it to false), then render the form again passing the updated model data. Heads up, problem incoming.

Resetting the model and passing it to the view seems to work just fine. Debugging the view shows that the view has the expected data. But, the input elements will still show the data that was input the step before.

This is what I don’t understand, to me it would be logical that the optional field should be empty and the checkbox unchecked, because the model data says so. But maybe I’m missing something (essential).

To Reproduce

Okay, I thought “I’ve tinkered quite a bit in that project, so I’m gonna try to reproduce it in a fresh project”. Done so and unfortunately I was able to reproduce it.

The project can be found here

The model class is called CheckBoxTestModel (I stumbled over checkboxes first, then wondered about text fields); the controller is coded in HomeController, views can be seen in Views/Home/Index.cshtml and Views/Home/PostText.cshtml. Apart from these files, it’s just the project template.

Further technical details

  • I’m using ASP.NET Core 5 with JetBrains Rider 2020.2 as IDE
❯ dotnet --info
.NET SDK (reflecting any global.json):
 Version:   5.0.100
 Commit:    5044b93829

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19041
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.100\

Host (useful for support):
  Version: 5.0.0
  Commit:  cf258a14b7

.NET SDKs installed:
  3.0.100 [C:\Program Files\dotnet\sdk]
  3.1.100 [C:\Program Files\dotnet\sdk]
  3.1.403 [C:\Program Files\dotnet\sdk]
  5.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download

Best regards

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
tbureckcommented, Nov 25, 2020

Hi,

no worries. This snippet works, but I have to say that it looks rather unsatisfying. I actually want to reset the model’s state in a method of the model itself instead of letting the controller handle this, so using ModelState can only be a workaround.

I’m probably not understanding how model binding actually works. Apparently @mkArtakMSFT thinks this is a Docs issue, so I’ll try to read into the code a little bit more and if there’s no cleaner solution to this than what you’ve written, I might come up with a different solution altogether.

Anyways, thanks so far 😃

Best regards

1reaction
tbureckcommented, Nov 23, 2020

Hi,

thanks for your response. Unfortunately your suggestion didn’t make a difference.

Best regards

Read more comments on GitHub >

github_iconTop Results From Across the Web

ASP.NET MVC Updated model data not showing in View
My problem is that the data displayed in the View are still the same (i.e. not updated according to modified model). I've read...
Read more >
Tutorial: Update related data with EF in an ASP.NET MVC ...
In this tutorial you'll update related data. For most relationships, this can be done by updating either foreign key fields or navigation ...
Read more >
View is not updating values in Asp.Net MVC.
Sometime view is not updating values in Asp.Net MVC. I have recently facing this issues and I thought let me write blog on...
Read more >
Model binding not working on submitting razor ... - YouTube
Why model binding does not work in MVC as expected, if we submit a razor form that contains a foreach loop. The list...
Read more >
ASP.NET MVC Tutorial - Data Binding and Hierarchical ...
Learn the true power of ASP.NET MVC default model binding and how you can use it to collect data for hierarchical entity relationships....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found