views:

47

answers:

1

i have a strongly typed asp.net-mvc view and the Model has a .Links property.

Links is an array of link, which has a URL, a Description and an ID.

i have a bunch of links. Every links has a list of tags and those tags are layed out like this:

<a id="98" href="www.ibm.com>IBM</a><b class='dblclick'>Tags: Software</b>
<a id="99" href="www.microsoft.com>MIcrosoft</a><b class='dblclick'>Tags: Software</b>
<a id="100" href="www.apple.com>Apple</a><b class='dblclick'>Tags: Software, Fruit</b>

(www.ibm.com being the URL, IBM being the Description, and "98" being the ID)

i have the following jquery code:

<script type="text/javascript">
    $(document).ready(function() {

        $(".dblclick").editable('<%=Url.Action("UpdateSettings","Links", new { site = Model.Links[0].ID }) %>', {
            tooltip: "Doubleclick to edit...",
            event: "dblclick",
            style: 'display: inline; width:400px;'
        });

    });

the issue that i dont want to have: Model.Links[0].ID hard coded, i want to pass in the index into the method (98 in this case). Since every link has the "dblclick" class, how do i pass in the exact link or index into this array so i can stick in the ID field into this editable method?

+2  A: 

You should get away with replacing:

Model.Links[0].ID

With

$(this).prev("a").attr("id")

Although it would be better if you didn't have to traverse sideways as it is destined to cause problems. If you could put the ID on the "b" element, it would be as easy as

<a id="98" href="www.ibm.com>IBM</a><b class="dblclick" id="98">Tags: Software</b>

...

$(this).attr("id")

Also, you may need to think about those IDs as HTML id attributes aren't supposed to start with a number!

Sohnee
i moved the id over to the <b> class but this seems to be causing a run time error. see below $(".dblclick").editable('<%=Url.Action("UpdateSettings","Links", new { site = $(this).attr("id") }) %>', {
ooo
Slight syntax error - the JavaScript needs to be in the non MVC bit of code (i.e. $(this).attr("id") needs to be outside of the <% %> bit!
Sohnee