comment 0

Handling Concurrency in ASP.NET Core MVC

Also see my previous blog post – More work with my ASP.Net Core MVC web app demo. Concurrency conflicts happen when the record you are editing is updated by someone (or a process) outside of your edit screen. Potentially if left unattended then changes can be lost. The following post does a mostly good job of explaining concurrency and shows the coding required to get started (in ASP.NET Core MVC)  – Handling concurrency conflicts – EF Core with ASP.NET Core MVC tutorial. I chose optimistic concurrency for my demo app.

One of the things I discovered (but not confirmed – no documentation found) was that if you use the html helper Html.HiddenFor you cannot set the value (and it binds and pulls it from the database) and you have to use Html.Hidden.


@Html.Hidden(“HiddenField”, “Some Value”) // works
@Html.HiddenFor(model => model.HiddenField, new { id = “HiddenField”, Value = “Some Value” }) // does not work for me

This brings up a good point to make. Never trust a hidden or read only field to not be changed. Hidden and read-only fields on the client side are just for aesthetics. Also read somewhere that all validation should (also) take place on the server and never trust input from the client. Read that you can encrypt it (as an option) if you do not want the user to change it. The example code from the link above says, “The view stores the original RowVersion value in a hidden field, and this method receives that value in the rowVersion parameter.” In this situation it does not matter because being able to change the rowVersion parameter just affects concurrency checks but what about other situations? How can you pass unchangeable data from View to Controller? Will that ever be needed? I was thinking I could use ViewBag but that is only one way from the Controller to the View. This seems like an interesting thing to think about and needs some thought so I will leave it for another blog post.

The other thing I came across was AsNoTracking. More info about this here – No-Tracking Queries. Can’t confirm if this is needed in loading data for the Edit in this code as I now cannot reproduce the error I was initially getting related to this. So at this point I had concurrency notices when Save is clicked. Later I implemented push notices which I will cover in a future blog post. Share your experiences and thoughts with us in the comments below.

Handling Concurrency in ASP.NET Core MVC - 1

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s