最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net mvc - How to call a server-side function from javascript in MVC? - Stack Overflow

programmeradmin0浏览0评论

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 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?

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 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?

Share Improve this question asked Apr 19, 2013 at 7:06 NirmanNirman 6,79321 gold badges80 silver badges147 bronze badges 1
  • Use Ajax !!! My apprentice ;-) – hackp0int Commented Apr 19, 2013 at 7:11
Add a ment  | 

2 Answers 2

Reset to default 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
    }
})

You can use ajax for that (jQuery):

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

This is razor syntax...

发布评论

评论列表(0)

  1. 暂无评论