views:

54

answers:

2

Hi All, i just started building a small test app to help me learn MVC. i have a view that displays user comments. Under each comment i would like to have a reply action link. Clicking on the link should return a small form for adding a comment directly above the reply link.

What is the general approach for this? I'm imaging the form would be a partial view that i can somehow return using the reply link. Thanks for any help!

A: 

Using jQuery to retrieve and post the forms in partial views is how I would do it.

Just return partial view to be loaded by jQuery load:

$('#comment').load('/MyController/ActionReturnsPartial');
Maxwell Troy Milton King
Hi Maxwell, i think this is what i am looking for.. so my reply link would basically call a script that passes the commentId and executes the above js??
yes. wire up your reply links click event to run the load script above.
Maxwell Troy Milton King
ok thanks! what does the action look like that returns a partial view?
got it working. thanks!
A: 

You should not have to retrieve anything from the server if the user is not providing any extra information along with the retrieval.

Instead of retrieving the form when the user clicks, just let the page render a form below each comment. Put the form in a div with style="display: none;". Then, when the user cliks the link, use jQuery to show the form. Something like

$('.commentlink').click(function(){
  $(this).closest('div').find('.formdiv').show();
});

You may also be able to use jQuery's .toggle() method.

Rune
but if you listing 100 comments or more, it'll increase page size. I think creating new form in client-side better then this solution.
cem
You would only need 1 reply form, using jQuery as Rune suggests you can wire every link up to that single form. It'd be no larger in page size than any other method.
TreeUK
Yep you're right, single form fix this. but Rune suggestion's not single form as i see, rendering new form for each comment when click reply, just showing comment form not moving.
cem
That is a valid point. But if all we are doing is adding a few <form> tags, I don't think it will be a problem (but I obvoiusly cannot know). If you are worried about the size of the page just insert the form in the div once the user clicks the link (something like $('.formdiv').html('<form blah blah><input blah blah /></form>'); )
Rune
This answer would seem to work as well. I wonder if there is any advantage to using this? I don't understand enough with how the controller works with partial views to know. But thanks for your answer!
i don't have enough rep to give you a vote. sorry