如何留住在asp.net mvc的页面刷新后注射局部视图 [英] How to retain an injected partial view after page refresh in asp.net mvc

查看:138
本文介绍了如何留住在asp.net mvc的页面刷新后注射局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ASP.NET MVC 4 的jQuery 。我有我的观点一个按钮控件。当点击它注入的局部视图进入我的视野。当我刷新页面,然后局部视图已经一去不复返了。

I am using ASP.NET MVC 4 and jQuery. 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.

我的HTML标记:

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

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

我的jQuery与AJAX调用返回的局部视图返回的HTML:

My jQuery to return the partial view with an AJAX call to return the 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');
          }
     });
});

我的操作方法:

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

     return PartialView("_RuleInformation", viewModel);
}

我的部分观点:

@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.

一,以达到您所使用寻找的是如何 sammy.js

One of the ways to achieve what you are looking for is by using sammy.js

在点击按钮就可以使用设置hashurl sammy.js (例如:'#/ partialview )和页面刷新hashurl部分保持不变(但不会去服务器)。然后你就可以相应地操纵客户端code

On click of button you can set the hashurl using sammy.js (e.g.: '#/partialview') and on page refresh hashurl part stays intact (but does not go to server). And then you can manipulate the client code accordingly


  1. 在您的网页以sammy.js参考。

  2. Intialize萨米像下面

  1. Take sammy.js reference in your page.
  2. Intialize sammy like below

     var app = $.sammy('body', function (context){

           this.get('#/PartialView1', function () {
                fnLoadPartialView();
            });

});


  • 改变检索与HREF =PartialView1

  • change Retrieve to with href='PartialView1'

    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。

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

    这篇关于如何留住在asp.net mvc的页面刷新后注射局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆