如何从类文件传递字符串值到Mvc控制器

本文关键字:字符串 Mvc 控制器 文件 | 更新日期: 2023-09-27 18:16:57

这里我使用的是Repo类,因为我写了一些逻辑。当逻辑成功时,我想将字符串msg传递给mvc控制器请帮助我

Repo.cs

 public void validateUser(Auth aut)
        {
            var xx=aut.Email;
            var xr=db.Auths.Where(rr=>rr.Email == xx).FirstOrDefault();
            if (xr != null)
            {
                var x = (from n in db.Auths
                         where n.Email == xr.Email && n.Password == xr.Password
                         select n).FirstOrDefault();
                if (x != null)
                {
                    var xz = (from n in db.Auths
                             where n.Email == xr.Email && n.Password == xr.Password && n.Active == aut.Active
                             select n).FirstOrDefault();
                    if (xz != null)
                    {
                        string Acc = "Your Account Activated....";
                    }
                }
            }
            else
            {string ddd = "Your Account not Activated....";}}

Controller.cs

      Repo objrepo = new Repo();
 public ActionResult Login(Auth aut)
        {
            if (ModelState.IsValid)
            {
               objrepo.validateUser(aut);
                ViewBag.Success = "Success.....";
            }
            else
                ViewBag.msg = "Invalid.....";
            return View();
        }

如何从类文件传递字符串值到Mvc控制器

你可以试试:

public string validateUser(Auth aut)
    {
    string result = "Invalid Email Address ....";
        var xx=aut.Email;
        var xr=db.Auths.Where(rr=>rr.Email == xx).FirstOrDefault();
        if (xr != null)
        {
        result = "Invalid Password ....";
            var x = (from n in db.Auths
                     where n.Email == xr.Email && n.Password == xr.Password
                     select n).FirstOrDefault();
            if (x != null)
            {
            result = "Your Account is not Activated ....";
                var xz = (from n in db.Auths
                         where n.Email == xr.Email && n.Password == xr.Password && n.Active == aut.Active
                         select n).FirstOrDefault();
                if (xz != null)
                {
                    result = "Your Account Activated....";
                }
            }
        }
    return result;
    }

:

 public ActionResult Login(Auth aut)
    {
        if (ModelState.IsValid)
        {
           string result = objrepo.validateUser(aut);
            ViewBag.Success = result;
        }
        return View();
    }

将repo.cs文件的validateUser方法的返回类型从void更改为string。从方法返回到控制器的消息

控制器文件

public ActionResult Login(Auth aut)
        {
            if (ModelState.IsValid)
               ViewBag.msg = objrepo.validateUser(aut);
            else
                ViewBag.msg = "Invalid.....";
            return View();
        }
使用

ViewBag。msg在视图文件。

谢谢,Hiral沙