'Update ASP.NET MVC Model value from javascript

In an ASP.NET MVC page, I'd like to change some valuee in the Model from javascript code when I click on an onClick button event.

I tried several solution but in any case the model is updated.

As exemple : In the model there are :

  • Firstname
  • LastName
  • Email.

In the razor page :

@Html.TextboxFor(model => model.LastName, new { id = "MyLastName" })

There is a button :

<button id="btnDelete" onclick="/*Code to update model*/ ">Click me</button>

In "Code to update model", I'd like change the model value for @Model.FirstName and @Model.Email via javascript.

I tried to assign Model.LastName to an HiddenFor :

@Html.HiddenFor(model => model.Lastname, new { id = "MyLastName" })

Get the value by javascript and change the value but the value in the Model is not updated.

Any idea ?

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source