Skip to content Skip to sidebar Skip to footer

How To Call A Server-side Function From Javascript In MVC?

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)

Solution 1:

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
    }
})

Solution 2:

You can use ajax for that (jQuery):

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

This is razor syntax...


Post a Comment for "How To Call A Server-side Function From Javascript In MVC?"