问题描述
我使用 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.
一,以达到您所使用寻找的是如何
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
- 在您的网页以sammy.js参考。
-
Intialize萨米像下面
- Take sammy.js reference in your page.
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的页面刷新后注射局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!