Warm tip: This article is reproduced from stackoverflow.com, please click
entity-framework modelstate

ModelState Validation in MVC

发布于 2020-04-09 22:59:47

the class has a property UserId which type of ApplicationUser and its required, and of course we can not pass it from view to controller because of security reasons.

Now when the controller checks the state of model the model is not in a correct state because there is no UserId Value and it returns the view back, if I use the view model the class has more than 50 properties and assigning the values from view model to class and then save it it is very tedious and difficult to do it for saving editing and so on, any advice to come out from this problem

thanks

Questioner
Mohammad
Viewed
60
Mohammad 2020-02-17 14:27

anyhow thanks for the cooperation for solving this problem the easiest way that i found to return a UserId from a class, just one line of code is required inside class constructor

public constructor()
{
  UserId =  ClaimsPrincipal.Current.Identity.GetUserId();
}