griegs
griegs

Reputation: 22760

Multiple same PartialsViews and Json

This may already have an answer here but I can't seem to find the right search string so..

I have, almost the same as the comments in SO, a page that has many instances of the same PartialView.

The partial view has a button in it that when pressed adds a comment associated to the product the PartialView represents.

I can easily make this work if I do a full post back and all the comments are updated but it's an ugly solution.

What I need is on the button that was pressed I need a Json call to the server. I then save the comment and pass back a new PartialView with a list of new comments.

I want to then only update the comments on the particular product that I added the comment to.

I use this in the javascript to assign to the button;

$(".clsTest").unbind("click").click(function(evt) {

But it only attaches this to the first instance of the button it finds not all.

I feel I'm missing some key element of knowledge here. I'm not even sure I'm asking this question correctly.

Upvotes: 0

Views: 73

Answers (2)

RaYell
RaYell

Reputation: 70414

Why don't you use jQuery live for that. You won't have to add event handler whenever you add a new element matching the selector because jQuery will do it for you.

$('.clsTest').live('click', function () {
    ...
});

Upvotes: 2

ten5peed
ten5peed

Reputation: 15890

I'm no jQuery ninja but I think you need to look at the jQuery '.each' method.

Maybe something like this(?):

$(".clsTest").each(
    function() {
        $(this).unbind("click").click(function(evt) {
            //do your stuff...
        });
    }
);

HTHs
Charles

Upvotes: 0

Related Questions