通过Sessions通过不同的层传递数据(DAL到View)

时间:2016-11-07 12:06:41

标签: c# asp.net asp.net-mvc visual-studio session

目标是从数据库中获取数据,并在以欢迎,(此处为此名称)登录时显示客户的姓名。我得到的只是一个空引用,所以我没有把它传递给它。

我通过此方法调用它来获取DAL命名空间中的customer名称:

public bool insertCusReg(CustomerCTX inCusReg)
    {
        var db = new BankContext();
        try
        {
                var newCus = new Customer();

                string salt = addSalt();
                var passwordAndSalt = inCusReg.Password + salt;
                byte[] passwordDB = makeHash(passwordAndSalt);
                newCus.Firstname = inCusReg.Firstname;
                newCus.Lastname = inCusReg.Lastname;
                newCus.SSN = inCusReg.SSN;
                newCus.Password = passwordDB;
                newCus.ConfirmPassword = inCusReg.ConfirmPassword;
                newCus.Salt = salt;

                db.Customer.Add(newCus);
                db.SaveChanges();
                return true;

        }

        catch (Exception)
        {
            return false;
        }
    }

从Controller调用此方法:

  public ActionResult Login()
    {
        return View();
    }


    [HttpPost]
    public ActionResult Login(CustomerCTX inCusReg)
    {

        var bankDb = new DBBankBLL();  // getting called from another method which is mentioned below       
        bool insertOK = bankDb.user_in_db(inCusReg);   

        if (insertOK)
        {

            Session["codeValid"] = true;
            Session["name"] = inCusReg.Firstname;
            return RedirectToAction("MyProfile", "Home");
        }
        else
        {
            Session["LoggedIn"] = false;
            ViewBag.LoggedIn = false;
            return View();
        }
    }

这是从上面调用的BLL部分:

 public bool user_in_db(CustomerCTX inCusReg)
    {
        var DBBankDAL = new DBBankDAL();
        return DBBankDAL.user_in_db(inCusReg);
    }

最后在此视图中输出客户名称:

@if (Session["codeValid"] != null && Session["name"] != null )
{

ViewBag.Message = "Welcome, " + Session["name"].ToString(); ;

<ul class="nav navbar-nav navbar-right">
    <li class="dropdown">
        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
            @ViewBag.Message
            @*@User.Identity.Name*@
            <span class="caret"></span>
        </a>
        <ul class="dropdown-menu" role="menu">
            <li><a href="@Url.Action("RegPay", "Home")"> Register Payement</a></li>
            <li><a href="@Url.Action("OverviewRegPay", "Home")"> Overview of Payement</a></li>
            <li><a href="@Url.Action("ListOfConfirmedRegPay", "Home")"> List of Confirmed Payement</a></li>
            <li><a href="@Url.Action("MyProfile", "Home")"> My Profile</a></li>
            <li class="divider"></li>
            <li><a href="@Url.Action("Logout", "Home")"> Log out  <span class="glyphicon glyphicon-log-out"></span></a></li>
        </ul>
    </li>
</ul>
}

模型是:

public class CustomerCTX
{
    //[Key]
    public int Id { get; set; }

    [Display(Name = "First Name:")]
    [Required(ErrorMessage ="Firstname is required.")]
    public string Firstname { get; set; }

    [Display(Name = "Last Name:")]
    [Required(ErrorMessage = "Lastname is required.")]
    public string Lastname { get; set; }

    [Display(Name = "Social Security Number:")]
    [Required(ErrorMessage = "Social Security Number is required.")]
    [RegularExpression(@"[0-9]{11}$", ErrorMessage = "SSN can only conatin numbers and must be 11 digits long.")]
    public Int64 SSN { get; set; }

    [Display(Name = "Password:")]
    [Required(ErrorMessage = "Password is required.")]
    [RegularExpression(@"[A-Z, a-z, 0-9]{6,}$", ErrorMessage = "Password must conatin minimum 6 characters")]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [Display(Name = "Confirm Password:")]
    [Compare("Password", ErrorMessage = "Please confirm your password")]
    [DataType(DataType.Password)]
    public string ConfirmPassword { get; set; }

}

仍然是编程和仍在学习的新生。 希望有人能够发现我出错的地方并非常感谢所有的帮助!

0 个答案:

没有答案