简体   繁体   English

url.action,jquery和mvc

[英]url.action, jquery, and mvc

I'm building an application that will shutdown a server, ping that server, and notify the user if the server has been shutdown. 我正在构建一个应用程序,它将关闭服务器,对服务器执行ping操作,并在服务器已关闭时通知用户。 If it does not shutdown successfully, it allows the user to "Try Again". 如果未成功关闭,则允许用户“重试”。 I can get the application to process the logic correctly the first time but when I implement the "Try Again" feature, it does not reprocess the Shutdown action. 我可以让应用程序第一次正确处理逻辑,但是当我实现“重试”功能时,它不会重新处理“关机”操作。 I think it must have something to do with caching but I don't know enough about it to know where to start looking. 我认为它必须与缓存有关,但我对缓存了解不足,无法知道从哪里开始寻找缓存。 Any ideas? 有任何想法吗?

Here is my code: 这是我的代码:

Controller 调节器

    //URL: Build/Progress  
    public ActionResult Progress()  
    {  
        return View();  
    }  

////URL: Build/MoveDevice  
public ActionResult ShutdownStatus()  
{  
    ImageInfo ImageInfo = new ImageInfo();  
    FarmInfo FarmInfo = new FarmInfo();  
    ProgressModel ProgressModel = new ProgressModel();  

    if ((Session["ShutdownStatus"] as string) == "Success")  
    {  
        ProgressModel.ShutdownStatus = 1;  
        return View(ProgressModel);  
    }  
    else  
    {  
        ImageInfo = svcImage.GetImageInfoByImageId(Session["ImageName"].ToString());  
        string Farm = ImageInfo.FarmId.ToString();  
        FarmInfo = svcFarm.GetFarmByFarmId(Farm);  
        svcImageSvc.ShutdownDevice(Session["Username"].ToString(), Session["Password"].ToString(), Session["Domain"].ToString(),  
            FarmInfo.farmName, ImageInfo.Device, ImageInfo.ImageHistory.Status, ImageInfo.PVSHost, ImageInfo.PVSAccount);  

        ProgressModel.ShutdownStatus = svcImageSvc.PingDevice(ImageInfo.Device);  
        return View(ProgressModel);  
    }  
}

Views (Progress.aspx) 视图(Progress.aspx)

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>  

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">  
    Progress  
</asp:Content>  

<asp:Content ID="Content5" ContentPlaceHolderID="HeadContent" runat="server">  

</asp:Content>  

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">  
    <h1>Progress</h1>  
    <hr />  

    <!-- Shutdown Status -->  
    <div class="panel" id="panel1"><img src="../../Assets/Images/load.gif" /></div>  
    <script type="text/javascript">  
        $.get('<%= Url.Action("ShutdownStatus", "Build", null) %>',  
        function (data) {  
            $('#panel1').replaceWith(data);  
        });  
    </script>  

</asp:Content>  

<asp:Content ID="Content3" ContentPlaceHolderID="HeaderContent" runat="server">  
</asp:Content>  

<asp:Content ID="Content4" ContentPlaceHolderID="FooterContent" runat="server">  
</asp:Content>

View (ShutdownStatus.asxc) 查看(ShutdownStatus.asxc)

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<PvsUtil.App.Core.Models.ProgressModel>" %>  

    <% using (Html.BeginForm())  
       { %>  
        <% if (Model.ShutdownStatus == 1)  
           { %>  
           <%: Session["ShutdownStatus"] = "Success" %>  
            <p>  
                Server shutdown succesfully  
            </p>  
            <% } %>  

        <% if (Model.ShutdownStatus == 2)  
            { %>  
            <p>Server did not shutdown within 1 minute.  Please check the server and try again.</p>  
            <%: Html.ActionLink("Try Again", "Progress", "Build") %>  
            <% } %>  

        <% if (Model.ShutdownStatus == 3)  
        { %>  
        <p>An error has occurred.  Please check the server and try again.</p>  
        <%: Html.ActionLink("Try Again", "Progress", "Build") %>  
        <% } %>  
     <% } %>  

I believe the caching is actually being done by jQuery. 我相信缓存实际上是由jQuery完成的。 Change the $.get to a $.post and see if it works. 将$ .get更改为$ .post,看看是否可行。 If it does, you can still use $.get, but you will need to set the option: 如果是这样,您仍然可以使用$ .get,但是您需要设置以下选项:

cache: false

HTH, Brian HTH,布莱恩

Try adding: 尝试添加:

[OutputCache( Location = OutputCacheLocation.None )]

to your controller (or each of the actions you don't want to be cached). 到您的控制器(或您不想缓存的每个动作)。

The other thing you want to do is wrap your javascript in a document.ready() handler. 您要做的另一件事是将您的JavaScript包装在document.ready()处理程序中。 That's not strictly necessary as your script occurs after the element that it references, but it's a good practice nonetheless -- and you could also move it to just above the closing body tag, but that's more of an optimization. 这并不是严格必要的,因为脚本会在它引用的元素之后出现,但这仍然是一个好习惯-您也可以将其移到主体标记的上方,但这更多的是优化。

<script type="text/javascript">
  $(function() { // execute on document ready   
      $.get('<%= Url.Action("ShutdownStatus", "Build", null) %>',   
      function (data) {   
          $('#panel1').replaceWith(data);   
      });
  });  
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM