简体   繁体   English

如何在ASP.NET MVC中刷新页面后保留注入的局部视图

[英]How to retain an injected partial view after page refresh in asp.net mvc

I am using ASP.NET MVC 4 and jQuery . 我正在使用ASP.NET MVC 4jQuery I have a button control on my view. 我的视图上有一个按钮控件。 When it is clicked it "injects" a partial view into my view. 单击它会将部分视图“注入”到我的视图中。 When I refresh the page then the partial view is gone. 当我刷新页面时,部分视图消失了。

My HTML markup: 我的HTML标记:

<button id="RetrieveButton" type="button">Retrieve</button>

<div id="RuleDetails">
     <div class="main-content">
          <p>Some text.</p>
     </div>
</div>

My jQuery to return the partial view with an AJAX call to return the HTML: 我的jQuery使用AJAX调用返回部分视图以返回HTML:

$('#RetrieveButton').click(function () {
     $.ajax(
     {
          type: 'POST',
          url: '@Url.Action("GetRuleDetails")',
          data: {
               systemCode: $('#SystemCodes').val()
          },
          dataType: "html",
          success: function (result) {
               alert('success');
               var domElement = $(result);
               $("#RuleDetails").html(domElement);
          },
          error: function (result) {
               alert('error');
          }
     });
});

My action method: 我的动作方法:

public ActionResult GetRuleDetails()
{
     RuleViewModel viewModel = new RuleViewModel();

     return PartialView("_RuleInformation", viewModel);
}

My partial view: 我的部分观点:

@model MyProject.ViewModels.Rules.RuleViewModel

<div class="main-content">
     <h2>Rule Details</h2>
</div>

I need this "injected" partial view to remain there if I were to refresh the page. 如果要刷新页面,则需要此“注入”局部视图保留在那里。 Currently it "resets" if I were to refresh it, and the partial that was injected is gone. 目前,如果我要刷新它,它将“重置”,并且注入的部分消失了。

It will not render that partial view again as it is does not remembers clicked state of button. 它不会记住按钮的单击状态,因此不会再次渲染该局部视图。

One of the ways to achieve what you are looking for is by using sammy.js 实现目标的一种方法是使用sammy.js

On click of button you can set the hashurl using sammy.js (eg: '#/partialview' ) and on page refresh hashurl part stays intact (but does not go to server). 单击按钮后,您可以使用sammy.js设置sammy.js (例如: '#/partialview' ),并在页面刷新hashurl部分保持不变(但不会转到服务器)。 And then you can manipulate the client code accordingly 然后您可以相应地操作客户端代码

  1. Take sammy.js reference in your page. 在页面中获取sammy.js参考。
  2. Intialize sammy like below 像下面一样初始化sammy

      var app = $.sammy('body', function (context){ this.get('#/PartialView1', function () { fnLoadPartialView(); }); }); 
  3. change Retrieve to with href='PartialView1' 用href ='PartialView1'更改为

  4.  function fnLoadPartialView (){ $.ajax( { type: 'POST', url: '@Url.Action("GetRuleDetails")', data: { systemCode: $('#SystemCodes').val() }, dataType: "html", success: function (result) { alert('success'); var domElement = $(result); $("#RuleDetails").html(domElement); }, error: function (result) { alert('error'); } }); } 

5. 5。

$('#RetrieveButton').click(function () {
     fnLoadPartialView ();
});

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

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