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

时间:2016-11-08 07:35:24

标签: c# asp.net-mvc

我在这里使用了Repo Class,因为我写了一些Logic.When那个逻辑成功我想把那个字符串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();
        }

2 个答案:

答案 0 :(得分:1)

你可以试试这个:

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();
    }

答案 1 :(得分:0)

在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 Shah