MVC3 JQuery 部分视图 Div 更新
这是我的场景:
我从 viewModel 创建了以下菜单
<ul>
<li id="1" class="menu-item-click">Item 1</li>
<li id="2" class="menu-item-click">Item 2</li>
<li id="3" class="menu-item-click">Item 3</li>
</ul>
列表中的每个项目都有“能力”知道它应该加载哪个部分视图(它以这种方式存储在数据库中)
我有以下代码来捕获每个列表项上的单击事件
$(".menu-item-click").click(function () {
//load the correct partial view
});
我的问题是我应该将信息存储在要加载的部分视图上吗?
我可以将其作为自定义属性存储在列表项 (li) 中。 (似乎不是最好的方法)
我希望有一种方法可以将列表项的 id 发送到一种可以返回正确的部分视图的“主”控制器。
任何帮助/指导将不胜感激。
Here is my scenario:
I have the following menu created from a viewModel
<ul>
<li id="1" class="menu-item-click">Item 1</li>
<li id="2" class="menu-item-click">Item 2</li>
<li id="3" class="menu-item-click">Item 3</li>
</ul>
Each item in the list has the "capability" of knowing what partial view it should load (it is stored in the database this way)
I have the following code to capture the click event on each list item
$(".menu-item-click").click(function () {
//load the correct partial view
});
My question is were should i store the information on what partial view to load?
I could store it in the list item (li) as a custom attribute. (Doesn't seem like the best way)
I wish there was a way to send the list item's id to a type of "master" controller that could return the correct partial view.
Any help/direction would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我更喜欢将数据附加到元素上,它被认为是一种用元素存储数据的语义方式,而不与 JS 代码混合,而且它被认为是从 HTML5 开始的标准实践。
I prefer attaching the data to the element, it is considered a semantic way of storing data with your elements without mixing in with JS code, plus it's considered a standard practice starting with HTML5.
您可以考虑在每个 li 上使用 data-* 属性。我知道你提到过你认为这不是最好的选择,但我认为它最终会让事情变得美好和简单。
You could consider using data-* attributes for this on each li. I know you mentioned you didn't think that was the best option, but I think it ends up keeping things nice and simple.
我不确定您想要存储什么类型的信息以及存储在什么信息上,但是如果您使用 jQuery 及其基于 DOM 的信息,那么存储信息的一个好方法是 $.data();
然后,调用:
您还可以在其中存储 JSON 内容,例如:
如果您要存储的数据基于某个元素,这就是我会这样做的方法。如果它是任意数据,您可以将其存储在 localStorage 中。如果这就是您想要的更多内容,请告诉我,我也可以写一个例子。
I'm not sure exactly what type of information you want to store and on what, but if you are using jQuery and its based on the DOM, a great way to store information is $.data();
Then, calling:
You can also store JSON stuff in there also like:
This is how I would do it if the data you want to store is based on an element. If it's arbitrary data you could store it in localStorage. If thats more of what you are looking for let me know and I can write an example of that too.