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.

TryUpdateModelAsync not working in ASP․NET Core 3.1

See original GitHub issue

Describe the bug

The TryUpdateModelAsync not working as expected in ASP․NET Core 3.1.

To Reproduce

  1. Setup demo project

    git clone https://github.com/doggy8088/TryUpdateModelAsyncProblem.git
    cd TryUpdateModelAsyncProblem
    dotnet run
    
  2. Send HTTP request to the demo action

    curl --location --request POST 'https://localhost:5001/WeatherForecast' --header 'Content-Type: application/json' --data-raw '{"Date": "2019-12-01", "TemperatureC": 1, "Summary": "N/A"}' -k -D -
    

The TryUpdateModelAsync should do the manual model binding under the hood. If I use [FromBody] in the parameter of the Action, it works. I’m trying to do the manual model binding in the action, but failed.

Here is the code snippet of the demo code:

[HttpPost]
public async Task<IActionResult> PostAsync()
{
	var value = new WeatherForecast()
	{
		Date = DateTime.Parse("2019-12-14"),
		TemperatureC = 100,
		Summary = ""
	};

	if (await TryUpdateModelAsync(value, ""))
	{
		return CreatedAtAction("Get", value);
	}
	else
	{
		return BadRequest();
	}
}

The original value’s Date property is 2019-12-14. When I do the request, I pass a new Date as 2019-12-01. So the expected result should be:

HTTP/2 201
date: Sat, 14 Dec 2019 11:19:38 GMT
content-type: application/json; charset=utf-8
location: https://localhost:5001/WeatherForecast
server: Kestrel

{"date":"2019-12-01T00:00:00+08:00","temperatureC":1,"temperatureF":33,"summary":"N/A"}

But the actual result is: ( The TryUpdateModelAsync is not working. )

HTTP/2 201
date: Sat, 14 Dec 2019 11:19:38 GMT
content-type: application/json; charset=utf-8
location: https://localhost:5001/WeatherForecast
server: Kestrel

{"date":"2019-12-14T00:00:00+08:00","temperatureC":100,"temperatureF":211,"summary":""}

Further technical details

  • ASP.NET Core version: 3.1
  • Include the output of dotnet --info: See below
  • The IDE (VS / VS Code/ VS4Mac) you’re running on, and it’s version: VSCode 1.39.2
.NET Core SDK (reflecting any global.json):
 Version:   3.1.100
 Commit:    cd82f021f4

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  18.04
 OS Platform: Linux
 RID:         ubuntu.18.04-x64
 Base Path:   /usr/share/dotnet/sdk/3.1.100/

Host (useful for support):
  Version: 3.1.0
  Commit:  157910edee

.NET Core SDKs installed:
  2.1.802 [/usr/share/dotnet/sdk]
  3.0.100 [/usr/share/dotnet/sdk]
  3.1.100 [/usr/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.14 [/usr/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.14 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.0 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.14 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

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

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
pranavkmcommented, Dec 30, 2019

Thanks @kishanAnem for the investigation. This is not a feature ASP.NET Core MVC has. https://github.com/aspnet/Mvc/issues/5659#issuecomment-270717629 goes in to some details as to why along with possible solutions to this.

1reaction
kishanAnemcommented, Dec 29, 2019

@doggy8088 yeah it’s right working MVC 5. In Asp.net community standup I think @DamianEdwards said “ removed that feature consciously”.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Timeout calling TryUpdateModelAsync on ASP.NET Core ...
One workaround that is listed there is to disable lazy loading when calling TryUpdateModelAsync. try { _context.ChangeTracker.LazyLoadingEnabled ...
Read more >
ControllerBase.TryUpdateModelAsync Method
A Task that on completion returns true if the update is successful. Attributes. NonActionAttribute. Applies to. ASP.NET Core 8.0 and other versions ...
Read more >
EF Core Tips: Make sure to call Update when it is needed!
Use TryUpdateModelAsync from ASP.NET Core to set values into the entity instance. EF will detect that these values have been set and mark...
Read more >
.NET6 Razor Pages CRUD Operations With Entity ...
In this article, we will do a small demo of the AspNetCore 6 Razor Pages CRUD operation. Razor Pages: Razor Page is a...
Read more >
How to unit test method with TryUpdateModelAsync invovled ...
Coding example for the question How to unit test method with TryUpdateModelAsync invovled in ASP.NET CORE Razor Page Web App?-.net-core.
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