Marius Vasile

Marius Vasile

  • 592
  • 1.9k
  • 146k

ASP.NET Core Razor - Post input, variable and string values

May 31 2020 9:38 AM
I am trying to save to table three values, one coming from cshtml input
  1. <div class="col-2">  
  2. <select class="form-control border-warning" style="font-size:12px" asp-for="PTWContent.PTWType" asp-items="Html.GetEnumSelectList<RoSafety.Models.PTW.PTWType>()">  
  3. <option value="">Permit to Work Type</option>  
  4. </select>  
  5. <span asp-validation-for="PTWContent.PTWType" class="text-danger"></span>  
  6. </div>  
one from a variable and one string from cshtml.cs
  1. var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);  
  2. var addPTW = new PTWContent();  
  3. if (await TryUpdateModelAsync(addPTW, "PTWContent", s => s.PTWType, s => s.UserID == userId, s => s.WorkComplete == "No"))  
  4. {  
  5. _context.PTWContents.Add(addPTW);  
  6. await _context.SaveChangesAsync();  
  7. return RedirectToPage("./PTW");  
  8. }  
The error I get is
An unhandled exception occurred while processing the request.
InvalidOperationException: The passed expression of expression node type 'Equal' is invalid. Only simple member access expressions for model properties are supported.
Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingHelper.GetPropertyName(Expression expression)

Answers (3)