0

I have some belows , please help me how i can get Session in Javascript code from Controller ?

public ActionResult Login(FormCollection f)
    {
        string sAcount = f["txtAccount"].ToString();
        string sPassword = f.Get("txtPassword").ToString();

        tblCustom cs = db.tblCustoms.SingleOrDefault(n=>n.Account==sAccount && n.Password==sPassword);

        if (cs != null)
        {

            Session["Account"] = cs;

            return View();

        }

        return View();

    }

and JS code is

        <script > 

       $('#btnSendMsg').click(function () {

                  var msg = $("#txtMessage").val();

                  alert('Hello' + Session["Account"] );
                 });   

    <script/>

the result is alert stil is not working, help me.

TienNguyen
  • 31
  • 1
  • 5
  • You are setting a `tblCustoms` object to session, not a string. So read it from session and use the property value for your alert. You may use `@` prefix to start a c# code block. – Shyju Dec 04 '17 at 20:10
  • Actually , i had set Controller Session["Account"]= "abc"; , and JS alert('Hello' + @Session["Account"]) , but i still is'not working , please – TienNguyen Dec 04 '17 at 20:34

2 Answers2

0

Although this doesn't directly answer your question, the preferred approach is to create ViewModels while passing and retrieving parameters.

Create a LoginViewModel:

public class LoginViewModel {
   public tblCustoms Customs { get; set; }
   //other stuff you have, you might consider moving account and password here too, 
   //instead of capturing with textbox names

   //public string Account { get; set; }
   //public string Password { get; set }
}

Pass that instead to the view.

public ActionResult Login(FormCollection f)
{
    string sAcount = f["txtAccount"].ToString();
    string sPassword = f.Get("txtPassword").ToString();

    var cs = db.tblCustoms.SingleOrDefault(n=>n.Account==sAccount && n.Password==sPassword);

    if (cs != null)
    {
        Session["Account"] = cs;

        //return View(); you don't need this line
    }

    return View(new LoginViewModel() { Customs = cs });

}

Add to top of your view:

@model YourNameSpace.LoginViewModel

And in the javascript:

<script>
   $('#btnSendMsg').click(function () {
        var msg = $("#txtMessage").val();
        alert('Hello ' + @Model.Customs );
    });
<script/>

As an alternative to all of these, you can use ViewBag. In the controller method, assign it to any name:

ViewBag.Customs = cs;

Then call it in the view:

alert('Hello ' + @ViewBag.Customs );

In order to use Session in your view, try this:

@Session["Account"].ToString();
Mithgroth
  • 1,114
  • 2
  • 13
  • 23
0

You should not update sessions many times, the type of data stored in Sessions are User Roles, Page Permissions and other global information. Once the login is done you should set login cookie. For login, you should use FormsAuthentication cookie.

Follow set Forms authentication to set forms authentication cookie. Or check this link Create Forms Authentication cookie.

In the page use

alert("@HttpContext.Current.User.Identity.Name");
Rudresha Parameshappa
  • 3,826
  • 3
  • 25
  • 39