N
N
niyaz_z2019-09-16 11:20:52
ASP.NET
niyaz_z, 2019-09-16 11:20:52

How to call a controller by another controller's method in ASP NET MVC?

I have a Home controller with a Login method

public class HomeController : Controller
         //метод Index
        public ActionResult Login(User user)
        {
            if (userAvail)
            {
                return RedirectToAction("Index", "Account", user);
            }
            else{
                  return View("Index");
                  }
        }   
    }

I also have an Account controller with a single Index method.
public ViewResult Index()
        {
            ViewBag.info = "You have 0 messages";
            return View("Account");
        }

I want the Account controller to be triggered when the Login method of the Home controller is called.
View Account.cshtml:
@model Login.Models.User
<div>
        <h1>Welcome @Model.Name! </h1>
        <p>@ViewBag.info</p>
    </div>

I'm trying to redirect to the Index method of the Account controller upon successful login, but it says that the view was not found.
In ASP NET MVC quite recently, I wanted to write a simple authorization application while studying it, but I ran into such a problem. I am aware of the concept of MVC and therefore do not write account logic in the Home controller method.
I would be happy with an intelligent solution and, if possible, advice on organizing client-server applications in asp.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Ascar, 2019-09-16
@Ascar

Your public ViewResult Index() method does not accept anything, and you pass it as the 3rd parameter. Remove the 3rd option. And authorization is forwarded through User.Identity.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question