For example, I have the EliminaUsuarios controller whose view is EliminarUsuarios.cshtml and another where the controller is Users and the view is Users.cshtml. What I want is to pass the value of ViewBag.Error in EliminateUsuarios (controller) to Users (View) so that I can show the error
namespace Sistema.Controllers
{
public class Usuarios : Controller
{
public ActionResult Usuarios()
{
return(db.Usuarios.Tolist())
}
public ActionResult EliminarUsuario
{
// ?????????????????aquí regresar a la vista
// Usuarios con el valor de ViewBag.Error
}
}
}
/////////////Vista Usuarios
@model IEnumerable<Sistema.Models.Usuario>
@{
ViewBag.Title = "Usuarios";
}
///Recibir valor de ViewBabag
@ViewBag.Error
Your class should be called
UsuariosController
instead ofUsuarios
Since you are just starting out with
ASP.net MVC
, I recommend you visit the following workshop that I did the previous month: https://www.youtube.com/watch?v=6LZG76aLykYYou have two options:
1) Return a RedirectToAction, which will execute the code
Action
you called:2) Directly return a
View
different one to the one that corresponds to theAction
, passing its name (Returns the View without going through theAction
Usuarios
):