Change a property value after aspnet click

Asked

Viewed 33 times

0

I got all the CRUD done, but I wish when I click the button "approve" in my View where you are listing Registered Data, change only the property Situation. I put by default, so she always gets "Pending" on Actionresult Registration.

[HttpPost]
    public ActionResult Cadastrar(Reserva reserva)
    {

        reserva.Situacao = "Pendente";


        _RRE.Inserir(reserva);

        return RedirectToAction("Index");

    }

So far so good. And in My Index I put a button that sends to my Actionresult Approved. The idea would be to use the same logic, placing for my property Situation receive "Approved" when the 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 Listagem

Result after clicking on "Approve" View ActionResult Aprovado

Note that he received "Approved", but the other values came null.

1 answer

0


It is returning to the form with null values because the Modelstate of your view is invalid in this case 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 is because the post you’re doing by clicking the Approve button is not submitting a valid representation of the object the action is waiting for in the controller.

In this case either you create another Viewmodel specific to the action or change its condition, ensuring that you are getting at least the attribute Id_Usuario and check the validity of the operation by him.

  • Leandro, but in the creation of Action, I’m doing it right ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.