ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,157 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have a model below:
public class Learning
{
public bool active { get; set; } = false;
}
in learning.cshtml
<div class="form-check form-switch form-check-primary">
<input type="checkbox" class="form-check-input" id="active" for-asp="active" tabindex="4" onclick="activeClick(this);" @(Model.active ? "checked" : "") />
</div>
and I try to update active value using javascript
<script>
function activeClick(cb) {
@Model.active = cb.checked;
}
</script>
and in controller
[HttpPost]
[Route("/User/Add_Update")]
public IActionResult Add_Update(Views.Learning learning)
{
bool active = learning.active; // THIS IS NOT WORKING, the active value keeps false
why the active value keeps false?, what I missed?
JavaScript runs on the client, razor code runs on the server. JavaScript can not update the @默 , because the server request is over.
You use case is not clear. If a form submit includes the checkbox no JavaScript is required.
you can use hidden field <input type="hidden"> and bind it to the target property of the model