会话变量返回null

时间:2017-10-12 13:52:16

标签: c# asp.net-mvc linq session-variables

我有一个登录页面,我尝试在我的会话中存储一些数据,但是当我运行项目时,它给出了我在屏幕截图中看到的错误:enter image description here

我试图调试我的代码,如截图所示,它显示我No_和E_mail都是null,即使我检查了我的数据库和那些列。 No_和E_mail都有数据: enter image description here


控制器中的代码:

[HttpPost]
public ActionResult Login(string Mail)
{     
    using (DataContext db = new DataContext())
    {
        ED_data_A_S_Contact con = new ED_data_A_S_Contact();
        ED_data_A_S_Contact_Business_Relation cb = new ED_data_A_S_Contact_Business_Relation();

        var user = from cbr in db.Contact_Business_Relation
                   join c in db.Contact
                   on cbr.Contact_No_ equals c.Company_No_ into f
                   from c in f.DefaultIfEmpty()
                   //where c.E_Mail == Mail
                   select new
                   {
                       Mail = c.E_Mail
                   };

        if (user != null)
        {
            Session["No_"] = cb.No_.ToString();
            Session["Email"] = con.E_Mail.ToString();
            FormsAuthentication.SetAuthCookie(Mail, false);

            return RedirectToAction("Index");
        }    
        else
        {
            ModelState.AddModelError("", "Kunde nr er ikke gyldig");
        }
    }

    return View();
}

public ActionResult LoggedIn()
{   
    if (Session["No_"] != null)
    {   
         return View();   
    }
    else
    {
        return RedirectToAction("Login");
    }
}

Contact_Business_Relation:

public string No_ { get; set; }

联系人:

public string E_Mail { get; set; }

视野中的守则:

@model ED_data_A_S_Contact

    <div class="login-wrap">
        <div class="login-html">
            <input id="tab-1" type="radio" name="tab" class="sign-in" checked><label for="tab-1" class="tab">Log ind</label>

            <input id="tab-2" type="radio" name="tab" class="sign-up"><label for="tab-2" class="tab"></label>

                <form class="login-form" method="POST" enctype="multipart/form-data" action="/Account/Login/">
                    @*@Html.AntiForgeryToken()*@
                    <div class="sign-in-htm">
                        <div class="group">
                            <label for="No_" class="label">Kunde ID</label>
                            <input style="color:black;" id="Email" name="Mail" value="" required="" type="email" placeholder="Kunde ID ..." class="input">
                            <button style="float: right;margin-top: -36px; background: transparent; border: none;margin-right: 11px;"  type="button" id="eye">
                                <img src="https://cdn0.iconfinder.com/data/icons/feather/96/eye-16.png" alt="eye" />
                            </button>

                            @*@Html.ValidationMessageFor(u => u.CustomerID)*@
                        </div>


                        <div class="group">
                            <input type="submit" class="button" value="Log på">
                        </div>
                        <span style="color:#dadada">@*@Html.ValidationSummary(true)*@</span>
                        <div class="hr"></div>

                        <div class="foot-lnk">

                            <a target="_blank" href="#">Problem med login?</a>
                        </div>
                    </div>

</form>

        </div>
    </div>

任何人都可以指引我朝着正确的方向前进吗? THX

1 个答案:

答案 0 :(得分:1)

ED_data_A_S_Contact_Business_Relation cb = new ED_data_A_S_Contact_Business_Relation();

在访问它们之前,您实际上似乎没有在任何地方设置此对象的属性。你所做的只是新对象。所有属性都有其默认值。

也许您应该访问user.Mail?在这种情况下,您还应该设置其他属性。

试试这个:

// Populate the user variable so we can use it below to set session variables
var query = from cbr in db.Contact_Business_Relation
            join c in db.Contact
            on cbr.Contact_No_ equals c.Company_No_ into f
            from c in f.DefaultIfEmpty()
            where c.E_Mail == Mail
            select new
            {
                Mail = c.E_Mail,
                No = c.No // Or whatever the relevant property is in c or cbr
            };

var user = query.FirstOrDefault();

            if (user != null)
            {
                 Session["No_"] = user.No.ToString();
                 Session["Email"] = user.Mail.ToString();
                 FormsAuthentication.SetAuthCookie(user.Mail, false);

                 return RedirectToAction("Index");
            }