Send succes or error messages in c # mvc

1

Hello community, I am working on a project with c # in mvc, well within my controller are my actions of grading, editing and eliminating what I want is to capture the error or success messages after each action I have something but it does not show me in my template the messages do it in the url here my code

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult RegistroEditar(FormCollection form)
    {
        SEG_ROL_DTO oRol = new SEG_ROL_DTO
        {
            id_rol = Funciones.CheckInt(form["id_rol"]),
            nombre = form["nombre"],
            descripcion = form["descripcion"]
        };

        try
        {
            string estadoForm = oRol.id_rol > 0 ? "E" : "N";

            SegRolBL.Grabar(estadoForm, oRol);
            return RedirectToAction("Lista", "Rol");
        }
        catch (ApplicationException x)
        {
            ViewBag.mensaje = x.Message;
            return RedirectToAction("RegistroEditar", "Rol", new { mensaje = x.Message, identificador = 1 });
        }
        catch (Exception e)
        {
            return RedirectToAction("RegistroEditar", "Rol", new { mensaje = e.Message, identificador = 2 });
        }
    }

Thanks for your help

    
asked by Jonathan Cunza 23.02.2018 в 16:43
source

2 answers

0

If the Actions of each RedirectToAction are in your controller, what you are doing is redirecting the corresponding action by passing an object by parameter, what you must do in this case is in each action return a view to which you pass a corresponding model, to show the message.

example: for: return RedirectToAction("RegistroEditar", "Rol", new { mensaje = x.Message, identificador = 1 });

in your controller you must have the following action:

public ActionResult RegistriEditar(string mensaje, int identificador)

Here you can choose two options:

  • Use the ViewBag dynamic object to which you can add the two corresponding properties and then with Razor show in the view:

    • Within the action in question you assign the parameters to two properties of that object in the following way:

    public ActionResult RegistriEditar(string mensaje, int identificador)      {

    ViewBag.Mensaje = mensaje;

    ViewBag.Identificador = identificador;

    return View();

    }

  • and then in the view with Razor samples the message as follows

    <div>
      @ViewBag.Mensaje
    </div>
  • For this case you can see a model, a class which in the view you have to specify by placing the following on the top of your cshtml:

    @model MyModelName

  • And previously in the action you must return the following:

    return View(new MyModelName(mensaje,identificador));
    

    in the view to use the model you must do the following:

    @Model.Mensaje
    

    Remember that the MyModelName must define and implement it

    I hope it serves you.

    Greetings.

        
    answered by 23.02.2018 в 19:57
    0

    Look at this article on how to control errors: link

    In the server part you just have to check

    if(!ModelState.isValid()) return View(form)
    

    With this it will return to the view and in the view you have to put what it explains in that article or look for unobtrusive-validation in google.

    Example of validation in view:

    @Html.ValidationMessageFor(x => x.phone, "")
    
        
    answered by 09.03.2018 в 18:56