Is it possible to make CheckBox
invoke a controller method on click just like it does ActionLink
? Basically, I want to replace this ActionLink
:
@Html.ActionLink("Switch status", "SwitchTaskIsComplete", "Task", new {
taskId = task.TaskId,
isComplete = !task.IsComplete,
userId = Model.UserId
}, null)
with a @Html.CheckBox
, which invokes the same method
SwitchTasksIsComplete(int taskId, bool isComplete, int userId)
of TaskController
and uses its checked
property as isComplete
parameter every time it gets clicked.
You can use the HTML onclick
for this:
@Html.CheckBox("mycheckbox", new { onclick="triggerLink()" })
Use @Url.Action
instead of @Html.ActionLink
to get just the URL:
<script>
function triggerLink() {
var theUrl = '@Url.Action("SwitchTaskIsComplete", "Task", new {taskId = task.TaskId, isComplete = !task.IsComplete, userId = Model.UserId}, null)';
window.location = theUrl;
}
</script>
You could also put the whole expression inline in the attribute:
@{
var url = Url.Action("SwitchTaskIsComplete", "Task", new {taskId = task.TaskId, isComplete = !task.IsComplete, userId = Model.UserId}, null);
}
@Html.CheckBox("mycheckbox", new { onclick="window.location = '" + url + "'" })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With