MD Ghouse MD Ghouse - 28 days ago 9
C# Question

How to pass string values from Class file to Mvc Controller

Here i'm using Repo Class in that i wrote some Logic.When that Logic success I want to pass that string msg to mvc controller please Help me

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

Answer

You could try this:

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

And this:

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

        return View();
    }
Comments