Modify a column with specific data using ASP.NET MVC5

1

I have a table with some users waiting for approval, create a button called approve, by pressing it sends you to for example

  

link < - 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 to click on (Approve) to change only the status value of my database for a specific value in my case the number 2.

It should be clarified that the current status of an unapproved user is 1 and I want that when approving said value 1 changes to 2 for example.

This is the line in the model public int status { get; set; }

    
asked by ByGroxD 06.04.2017 в 19:04
source

1 answer

2

Once you recover the Id of the collaborator you can access their properties and modify them:

var resultado = (from p in db.Collaborators
    where p.id == id
    select p).SingleOrDefault();

resultado.status = 2;
db.SaveChanges();

Finally there would be something like this:

public ActionResult Approved(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    var resultado = (from p in db.Collaborators
        where p.id == id
        select p).SingleOrDefault();

    resultado.status = 2;
    db.SaveChanges();

    ViewBag.id = id;
    return RedirectToAction("Index", "Collabotarors");
}

This is a demonstrative case of how it would work in general, but as a recommendation whenever you make any transaction, access to data, etc. it is good to use a try catch for any exception that is generated.

    
answered by 06.04.2017 / 19:13
source