I have a table with some users waiting for approval, I created a button called approve, pressing it sends you to for example
http://localhost:58074/Collaborators/Approved/1 <-- User ID
This is my view.
@{
ViewBag.Title = "Approved";
}
<h2>Approved</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-actions no-color">
<input type="submit" value="Aprobar" class="btn btn-default" /> |
@Html.ActionLink("Regresar", "Index", new { ViewBag.id })
</div>
}
This is my controller.
// GET: Collaborators/Approved/
public ActionResult Approved(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Collaborators collaborators = db.Collaborators.Find(id);
if (collaborators == null)
{
return HttpNotFound();
}
ViewBag.id = 1;
return View(collaborators);
}
I need that when clicking on (Approve) I change only the status value of my database for a specific value in my case the number 2.
It should be noted that the current status of a user NOT approved is 1 and I want that value 1 to change to 2 when approving it, for example.
This is the line on the modelpublic int status { get; set; }
Once you recover the
Id
collaborator you can access its properties and modify them:Finally it would be something like this:
This is a demonstrative case of how it would work in general, but as a recommendation whenever you carry out any transaction, data access, etc. it's good to use a
try
catch
for any exceptions that are thrown.