开发者

Partial View calling a method on the server every 5 second - No changes needed on the Page itself

I have a simple Partial View that I would like to automatically call a method on the server every 5 second.

Load the Partial View from the Master Layout:

@{Html.RenderAction("PingServer", "Account"); }

The Controller looks like this:

public ActionResult PingServer()
{
     return PartialView("PingServer");
}

The actual "_PingServer" Partial View is:

@{
    <script language="javascript" type="text/javascript"
                 src="http://code.jquery.com/jquery-latest.js">
    </script>
    <script language="javascript" type="text/javascript">
        $(function () {
            setInterval(Foo, 5000);
        });

        function Foo() {
            $.post("/Shared/ImHere.ashx", null, function () { });
        }    
    </script>
}

The Actual "ImHere.ashx.cs" looks like this:

public class ImHere : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
         SiteUser.ImHere();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

======================

THIS IS JUST NOT WORKING.

It actually used to work on MVC2 but NO MORE on MVC3. I understand the way Partial Views are handle in MVC3 is now a bit different with that concept of “_” (underscore) and not sure if that is the problem. I tried renaming that partial view _PingServer开发者_运维知识库 …but still NO effect.

Maybe it’s that I load the Partial View as Html.RenderAction instead of Html.RenderPartial ?

The idea is that that call needs to run every 5 seconds and not change a single thing on the page.

Any thoughts??


You don't need to use RenderAction, you can just use

@Html.Partial('partialName')

to render the partial view. Using this eliminates the need for the PingServer() action method.

Also your partial view shouldn't be within @ { } just

<script language="javascript" type="text/javascript"
             src="http://code.jquery.com/jquery-latest.js">
</script>
<script language="javascript" type="text/javascript">
    $(function () {
        setInterval(Foo, 5000);
    });

    function Foo() {
        $.post("/Shared/ImHere.ashx", null, function () { });
    }    
</script>

as it is just HTML


In your Foo javascript function you are calling the /Shared/ImHere.ashx handler so you shouldn't expect the /Account/PingServer action to ever be hit during the AJAX call. It will be hit only during the initial page load.

Master layout:

@{Html.RenderAction("PingServer", "Account"); }

Controller:

public class AccountController: Controller
{
    public ActionResult PingServer()
    {
        return PartialView();
    }
}

Corresponding partial (~/Views/Account/PingServer.cshtml). Notice the removal of @{} blocks as they are not necessary as well as using an Url helper to build the url to the server script to avoid hardcoding it which might break if your application is hosted in a virtual directory inside IIS:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
    $(function () {
        setInterval(Foo, 5000);
    });

    function Foo() {
        // here you are posting to an ASHX handler so don't expect
        // the controller action to ever be hit
        $.post('@Url.Content("~/Shared/ImHere.ashx")', null, function (result) { 
            // do something with the result of the AJAX call like update
            // some part of the DOM
        });
    }    
</script>


David, your last comment helped me finding the problem.

COMMENT: Also can you confirm your ImHere handler works independently of this whole process such as by navigating to the URL directly

PROBLEM: NAMESPACE MISMATCHING

When I added a NEW file to the project of type AHXS, Visual Studio by default gave it a namespace "ProjectName.DLLName.Folder" to both the HTML part and the CS.

I did rename the CS namespace to a different name (i.e ProjectName.Folder) but Visual Studio did not complained about it when i compiled.

Geeee ..such a novice mistake !!
Anyhow thanks all for helping out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜