@ Html.Action asp.net MVC

0

Good afternoon,

I have trouble going to a view, calling from another view.

I do it in the following way:

I'm in the OlvidoPassword view, and in the view there's a ViewBag.User, what I do is ask if the ViewBag is null, if it is, I send it to ValidateUser view.

@model ProyectoDeGrado.Models.Usuarios
@using db= ProyectoDeGrado.Models.Model1
@{ 
  string Usuario = ViewBag.Usuario;
 }
@if (string.IsNullOrEmpty(Usuario))
{
  @Html.Action("ValidarUserName", "Usuarios");
}
else
{
 @*Serie de codigo*@
}

When I run, it goes to the view that I want but the problem is the way it shows it, in the following image you see the errors:

    
asked by afar1793 26.03.2017 в 22:06
source

2 answers

1

This is because when you enter:

@Html.Action("ValidarUserName", "Usuarios");

You are rendering that view or partial view within the one you currently have. If you want to perform a redirection, you must use Response.Redirect , to make it look like this:

@if (string.IsNullOrEmpty(Usuario))
{
    @{ Response.Redirect("~/Usuarios/OlvidoPassword"); }
}
else
{
    @*Serie de codigo*@
}
    
answered by 26.03.2017 в 23:40
0

Although the problem is solved instead of the Html.Action place a Redirect ... The recommendation is to use the view for visualization issues and the controller to organize questions about what should be done in this or that case, that is, the logic of the flow in questions of the action to be executed.

This makes the code of your app more readable, maintainable in the not too distant future. The recommendation is if in your view you have some IF to fork the display, then do it in the controller (it depends on each particular case, but generally keeping the simple view helps a lot)

For example, in this case the OlvidoPassword driver would have to have the IF that is in the view to do the redirect or simply present this or that view.

But something much better, is that the driver has an Authorization attribute [ Authorize] to verify the user is validated, here you should not even ask for the user, the attribute will do this for you, and redirect directly to the login page configured in ASP.NET (obviously you can also modify it this page to which redirects). This is to help us not to be asking if there is any authenticated user (the question by the User other than null)

I hope it will help or guide you.

Links that can help you

answered by 27.03.2017 в 05:38