3

I am doing amendments in my MVC application in order to disallow users to open more than one tab/ window within a single session. I am taking reference of this article (click here) in order to do that. This article is written for asp.net whereas I need to implement this feature for ASP.NET MVC. I think all this should be possible in MVC, however, I am not sure what should I do to re-write this

if(window.name != "<%=GetWindowName()%>")

GetWindowName() is a function I have created in my Controller, and it returns a value of "WindowName" key from Session object. How can I read its value in above javascript?

1
  • Use Ajax !!! My apprentice ;-) Commented Apr 19, 2013 at 7:11

2 Answers 2

5

You can write a controller method for that:

public ActionResult GetWindowName()
{
  Session["WindowName"] = 
    Guid.NewGuid().ToString().Replace("-", "");
  return Json(Session["WindowName"].ToString());
}

Then call it through ajax:

$.get('@Url.Action("GetWindowName")', function(data){
    if(window.name != data) {
        // do what you need to do here
    }
})
Sign up to request clarification or add additional context in comments.

Comments

4

You can use ajax for that (jQuery):

$.get('@Url.Action("GetWindowName")', function(result){
    if(window.name != result)
    //...
});

This is razor syntax...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.