在ASP.NET MVC中实现后退按钮

时间:2014-07-17 05:00:15

标签: c# javascript jquery asp.net-mvc-5 asp.net-routing

我正面临在ASP.NET MVC应用程序中实现后退按钮功能的问题。
这是我的主页视图,为简单起见,让我们考虑它有一个div,它有img元素和div渲染PartialView2。 PartialView2是一个局部视图,它根据Model中的数据生成切片列表。

View1:
<div> remainig page content </div>
    <div>
    <img src="/path/to/img_back" id="img" />
    <div id="1">
    Html.RenderAction("PartialView2", "Home");
    </div>
    </div>
<div> remainig page content </div> 


PartialView2:
@model ICollection<Model>
@{
foreach(item in Model) {
<div class="tile">
<!-- tiles data generated using model data that I get from controller --> 
</div>
}
}

HomeController中:

//field
private Stack<ICollection<Model>> myStack = new Stack<ICollection<Model>>();

public PartialViewResult GetTiles(string id) {
 var nodes = new List<Model>();
 //make a call to database and generating the list of child nodes of type Model.
 // get the rows from the db which have parentid as the passed value.
 //for back button: myStack.push(nodes);
return PartialView("PartialView2",nodes);
}

//Method to call from jQuery on click of #img
public PartialViewResult BackButton() {
return PartialView("PartialView2",myStack.Pop());
}

当我点击一个磁贴时,我通过Jquery Ajax调用Home控制器中的GetTiles方法,并使用从ajax调用返回的结果填充$("#1").html(),这是相同的局部视图。

当我点击#img时,我从jQuery ajax调用BackButton()方法,在页面中加载#1 div的html。

型号:

string icon;
string title;
string parentid;
string id;
bool hasChildren;

通过这种设计,我只能选择遍历子节点并将它们表示为tile,但我需要实现一个后退按钮以遍历父节点。我在家庭控制器中创建了一个堆栈,并在每次调用GetTiles()方法时将元素推送到堆栈。但是对于每次调用控制器,都会实例化一个新堆栈。所以我的想法是在堆栈中的模型集合和每个回调按钮上弹出Model的集合并将其返回到视图不起作用。

你能帮我解决这个问题吗?用于保存父模型的任何内存数据结构? 如果我需要提供更多信息,请告诉我。

0 个答案:

没有答案