Change a value of a property after the aspnet

0

I have all CRUD done, but I would like it when I click the approve button in my View where you are listing the Registered data, just change the Location property. I put it by default, so it always gets "Pending" in the ActionResult Registration .

[HttpPost]
    public ActionResult Cadastrar(Reserva reserva)
    {

        reserva.Situacao = "Pendente";


        _RRE.Inserir(reserva);

        return RedirectToAction("Index");

    }

So far everything is beautiful. And in My Index I put a button that sends to my ActionResult Approved . The idea would be to use the same logic, putting my Situation property to receive "Approved" when ActionResult Approved is called, but does not work. The other values come null.

[HttpPost]
    public ActionResult Aprovado(Reserva item)
    {
        item.Situacao = "Aprovado";

        if (ModelState.IsValid)
        {
            // TODO: Add update logic here
            _RRE.Alterar(item);
            return RedirectToAction("Index");
        }
        else
        {
            return View(item);
        }
    }

View Index

Resultaaftertheclickonthe"Approve" button

Seethatithasreceived"Approved", but the other values have been null.

    
asked by anonymous 06.06.2018 / 20:06

1 answer

0

It is returning to the form with null values because the ModelState of your view is invalid so it returns to the form view and only redirects to index again if it is valid.

See in your code the section where you determine this behavior:

if (ModelState.IsValid)
{
    // TODO: Add update logic here
    _RRE.Alterar(item);
    return RedirectToAction("Index");
}
else
{
    return View(item);
}

This happens because the post you are doing when you click the Approve button is not submitting a valid representation of the object that the action is waiting on the controller.

In this case either you create another ViewModel specific to the action or change its condition, ensuring that you are receiving at least the Id_Usuario attribute and check the validity of the operation for it.

    
07.06.2018 / 01:54