Mvc并非所有代码路径都返回一个值 [英] Mvc not all code path returns a value

查看:90
本文介绍了Mvc并非所有代码路径都返回一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了MVC Action并且它给出了编译时错误,因为'并非所有代码路径都返回值。'



我尝试了什么:



I have created MVC Action and it gives compile time error as 'Not all code path returns value.'

What I have tried:

public ActionResult Submit(User u)
{
    List<User> uList = gc.Users.Include(p => p.Role).Where(p => p.UserName == u.UserName && p.UserPwd == u.UserPwd).ToList();
    List<User_Log> uUserLog = gc.User_Logs.Include(p => p.User).Where(p => p.UserName == u.UserName
       && p.Failed_DateTime.Value.Year == DateTime.Now.Year
                    && p.Failed_DateTime.Value.Month == DateTime.Now.Month
                    && p.Failed_DateTime.Value.Day == DateTime.Now.Day).ToList();

    if (uList.Count > 0 && uUserLog.Count < 3)
    {
        uList[0].IP = Request.UserHostAddress.ToString();
        if (uList[0].IsEnabled == true)
        {
            uList[0].Status = true;
            ub.UserLog(uList[0]);
            if (uList[0].PwUpdateDate == null)
            {
                return View("ChangePassword");
            }
            if (uList[0].UUpdateDate == null)
            {
                return View("UserDetails");
            }
        }
        else if (uList[0].IsEnabled == false)
        {
            uList[0].Status = false;
            ub.UserLog(uList[0]);
            ViewBag.Message("YOUR ACCOUNT IS DISABLED.");
            return View("Login");
        }
    }
    else if(uUserLog.Count<3)
    {
        u.IP= Request.UserHostAddress.ToString();
        u.Status = false;
        //uList[0].Status = false;
        ub.UserLog(u);
        ViewBag.Message="Invalid User Name Or Password. Your account will be locked out after three incorrect password attempts. \nTo avoid such situation, kindly click on FORGOT PASSWORD option for your user details.!";
        return View("Login");
    }
    else
    {
        ViewBag.Message="Your account is locked.To unlock send email to GEEM.SupplierReg@ge.com; ge.energy@datamatics.com";
        return View("Login");
    }

    //List<User> uList = uc.Users.ToList<User>().Where(p=>p.UserName==c.UserName && p.UserPwd==c.UserPwd);

}

推荐答案

请参阅我的代码内注释:



Please see my in-code comments:

            if (uList.Count > 0 && uUserLog.Count < 3)
            {
                uList[0].IP = Request.UserHostAddress.ToString();
                if (uList[0].IsEnabled == true)
                {
                    uList[0].Status = true;
                    ub.UserLog(uList[0]);
                    if (uList[0].PwUpdateDate == null)
                    {
                        return View("ChangePassword");
                    }
                    if (uList[0].UUpdateDate == null)
                    {
                        return View("UserDetails");
                    }
//                  else
//                  {
//   Here is an else-branch that you skipped
//   in which case your code would return nothing
//                  }
                }
                else if (uList[0].IsEnabled == false)
                {
                    uList[0].Status = false;
                    ub.UserLog(uList[0]);
                    ViewBag.Message("YOUR ACCOUNT IS DISABLED.");
                    return View("Login");
                }
//              else
//              {
//    Here is an else-branch that you skipped
//    in which case your code would return nothing
//              } 
            }
            else if(uUserLog.Count<3)
            {
                u.IP= Request.UserHostAddress.ToString();
                u.Status = false;
                //uList[0].Status = false;
                ub.UserLog(u);
                ViewBag.Message="Invalid User Name Or Password. Your account will be locked out after three incorrect password attempts. \nTo avoid such situation, kindly click on FORGOT PASSWORD option for your user details.!";
                return View("Login");
            }
            else 


这篇关于Mvc并非所有代码路径都返回一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆