text stringlengths 0 13.4k |
|---|
DueAt property. Then, use that date instead of always making |
new tasks that are due in 3 days. |
68 |
Complete items with a checkbox |
Complete items with a checkbox |
Adding items to your to-do list is great, but eventually you'll need to get |
things done, too. In the Views/Todo/Index.cshtml view, a checkbox is |
rendered for each to-do item: |
<input type="checkbox" class="done-checkbox"> |
Clicking the checkbox doesn't do anything (yet). Just like the last chapter, |
you'll add this behavior using forms and actions. In this case, you'll also |
need a tiny bit of JavaScript code. |
Add form elements to the view |
First, update the view and wrap each checkbox with a <form> element. |
Then, add a hidden element containing the item's ID: |
Views/Todo/Index.cshtml |
<td> |
<form asp-action="MarkDone" method="POST"> |
<input type="checkbox" class="done-checkbox"> |
<input type="hidden" name="id" value="@item.Id"> |
</form> |
</td> |
When the foreach loop runs in the view and prints a row for each to-do |
item, a copy of this form will exist in each row. The hidden input |
containing the to-do item's ID makes it possible for your controller code |
to tell which box was checked. (Without it, you'd be able to tell that some |
box was checked, but not which one.) |
69 |
Complete items with a checkbox |
If you run your application right now, the checkboxes still won't do |
anything, because there's no submit button to tell the browser to create |
a POST request with the form's data. You could add a submit button |
under each checkbox, but that would be a silly user experience. Ideally, |
clicking the checkbox should automatically submit the form. You can |
achieve that by adding some JavaScript. |
Add JavaScript code |
Find the site.js file in the wwwroot/js directory and add this code: |
wwwroot/js/site.js |
$(document).ready(function() { |
// Wire up all of the checkboxes to run markCompleted() |
$('.done-checkbox').on('click', function(e) { |
markCompleted(e.target); |
}); |
}); |
function markCompleted(checkbox) { |
checkbox.disabled = true; |
var row = checkbox.closest('tr'); |
$(row).addClass('done'); |
var form = checkbox.closest('form'); |
form.submit(); |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.