LINQ Query返回null值

时间:2018-02-24 04:51:08

标签: asp.net-mvc-4

当我使用Linq查询来获取或比较数据库中的数据时,它会在早上返回null值,但是现在它无法通过异常处理它。

模型

 public partial class CandidateRegistration
    {
        public int CandidateID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public bool RegisterAs { get; set; }
        public Nullable<bool> ApprovedBit { get; set; }
    }

public virtual DbSet<CandidateRegistration> CandidateRegistrations { get; set; }

控制器

public ActionResult Login(string emaillogin, string passwordslogin, bool registerAs, string Rememberme)
        {
            bool aprovedbitforemployer = false;
            if (registerAs == true)
            {
                var checkApprovedBitForEmployer = DB.EmployerRegistrations.FirstOrDefault(e => e.Email == obj.Email);
                aprovedbitforemployer = Convert.ToBoolean((checkApprovedBitForEmployer.ApprovedBit));
                if (aprovedbitforemployer != false)
                { 
                    if(checkApprovedBitForEmployer.Email == emaillogin && checkApprovedBitForEmployer.Passwords == passwordslogin)
                    {
                        Session["Username"] = checkApprovedBitForEmployer.Email.ToString();
                        Session["Passwords"] = checkApprovedBitForEmployer.Passwords.ToString();
                        Session["Employee"] = "Employer";
                    }
                }
                else
                {
                    TempData["Notpermission"] = "Your account is in activation process please wait...";
                }
            }
            else if (registerAs == false)
            {
                CandidateRegistration checkApprovedBitForEmplloyee = DB.CandidateRegistrations.FirstOrDefault(c => c.Email == emaillogin);
                aprovedbitforemployer = Convert.ToBoolean((checkApprovedBitForEmplloyee.ApprovedBit));
                if(aprovedbitforemployer == true)
                {
                    if (checkApprovedBitForEmplloyee.Email == emaillogin && checkApprovedBitForEmplloyee.Password == passwordslogin)
                    {
                        Session["Username"] = checkApprovedBitForEmplloyee.Email.ToString();
                        Session["Passwords"] = checkApprovedBitForEmplloyee.Password.ToString();
                        Session["Employee"] = "Employee";
                    }
                }
                else
                {
                    TempData["Notpermission"] = "Your account is in activation process please wait...";
                } 
            }
            else
            {
                TempData["Emailorpasswordmistakes"] = "Email or password incorrect";
            }
            return View();
        }

“checkApprovedBitForEmployer”返回空值it = n此变量。

查看

@using (Html.BeginForm("Login", "Home", FormMethod.Post))
{
            <div class="col-md-6 account">

                <h3>Login</h3>
                <span id="Emessage" onshow="show();"> @TempData["Notpermission"] </span>
                <label for="">Username or email address *</label>
                <div class="form-group">
                    <input type="email" name="emaillogin" id="emaillogin" class="form-control input-sm " placeholder="Email Address" style="width:70%; height: 35px; text-transform:none;">
                </div>
                <label for="">Password *</label>
                <div class="form-group">
                    <input type="password" name="passwordslogin" id="passwordslogin" class="form-control input-sm" placeholder="Password" style="width:70%; height: 35px; text-transform:none; ">
                    <span id="Emessage" onshow="show();" >@TempData["Emailorpasswordmistakes"]</span>
                </div>
                <div class="form-group">
                    @Html.DropDownList("RegisterAs", new List<SelectListItem>()
               {
                   new SelectListItem() { Text= "Register As", Selected = true },
                   new SelectListItem() { Text= "Employer", Value = "true" },
                   new SelectListItem() { Text= "Employee", Value = "false" }
               }, new { htmlAttributes = new { @class = "form-control" } })
                </div>

                <button>Login</button>
                <input type="checkbox" id="rememberme" name="Rememberme" value="forever"><span>Remember me</span>
                <br /> 
                <a href="#">Lost your Password</a>

            </div>
}

我正在使用html表单,我想使用它,但只告诉我它为什么不从数据库中获取数据。

0 个答案:

没有答案