2

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.

2
  • How do you get the partial view links? Does your model contain an IEnumerable<string>? Commented Apr 21, 2011 at 16:52
  • Yes. The MenuItem entity contains Id, MenuName, and MenuView Commented Apr 21, 2011 at 17:01

3 Answers 3

3

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.

<ul>
    @foreach (MyView item in Model.MyViews) {
        <li id="@item.id" class="menu-item-click" data-view="@item.href">Item 1</li>
    }
</ul>


$(".menu-item-click").click(function () {
    var view = $(this).data('view');
});
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah...this was my thought also.
0

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.

1 Comment

That was my first thought. Using the html5 data- attribute. Just wondering if there is another way I hadn't thought of.
0

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();

$.data('#someElement','theKey','some data goes here')

Then, calling:

$.data('#someElement','theKey')
//returns "some data goes here"

You can also store JSON stuff in there also like:

$.data(document.body,'data',{name:'Jim smith', email:'[email protected]'});

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.