text
stringlengths 0
13.4k
|
|---|
}
|
This code first uses jQuery (a JavaScript helper library) to attach some
|
code to the click even of all the checkboxes on the page with the CSS
|
class done-checkbox . When a checkbox is clicked, the markCompleted()
|
function is run.
|
The markCompleted() function does a few things:
|
70
|
Complete items with a checkbox
|
Adds the disabled attribute to the checkbox so it can't be clicked
|
again
|
Adds the done CSS class to the parent row that contains the
|
checkbox, which changes the way the row looks based on the CSS
|
rules in style.css
|
Submits the form
|
That takes care of the view and frontend code. Now it's time to add a
|
new action!
|
Add an action to the controller
|
As you've probably guessed, you need to add an action called MarkDone
|
in the TodoController :
|
[ValidateAntiForgeryToken]
|
public async Task<IActionResult> MarkDone(Guid id)
|
{
|
if (id == Guid.Empty)
|
{
|
return RedirectToAction("Index");
|
}
|
var successful = await _todoItemService.MarkDoneAsync(id);
|
if (!successful)
|
{
|
return BadRequest("Could not mark item as done.");
|
}
|
return RedirectToAction("Index");
|
}
|
Let's step through each line of this action method. First, the method
|
accepts a Guid parameter called id in the method signature. Unlike
|
the AddItem action, which used a model and model binding/validation,
|
the id parameter is very simple. If the incoming request data includes a
|
71
|
Complete items with a checkbox
|
field called id , ASP.NET Core will try to parse it as a guid. This works
|
because the hidden element you added to the checkbox form is named
|
id .
|
Since you aren't using model binding, there's no ModelState to check for
|
validity. Instead, you can check the guid value directly to make sure it's
|
valid. If for some reason the id parameter in the request was missing or
|
couldn't be parsed as a guid, id will have a value of Guid.Empty . If
|
that's the case, the action tells the browser to redirect to /Todo/Index
|
and refresh the page.
|
Next, the controller needs to call the service layer to update the
|
database. This will be handled by a new method called MarkDoneAsync
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.