views:

95

answers:

1

bascially, how should i do it now that using the asp:listview control is out of the question?

i am migrating from webforms to mvc and former implementation was in <asp:ListView...> so how should i do it now in terms of best user experience for the user? ie: i will need to ajax everything.

thanks

+1  A: 

You should use the standard list box now. If you need to return the list from your controller you return a SelectList [i think that's it].

To ajax the select list you can use say jQuery to add click and change events which will then call actionresults in your controller.

Edit

Ah right, there are a couple of ways. You could use a jQuery grid which will display the rows and allow you to have edit delet buttons. You then post back to an action, make the changes and come back. All Ajax of course.

The other way might be to write a couple of PartialViews. The first loops through your collection and the second displays the row. The last one would have say the buttons or a check box or whatever.

Then you again post back, via ajax, to an action result, make the changes and come back.

Your action result can return a fully rendered partial view so after you make your changes, return a new partial view of your data rows and replace the old with the new.

edit 2 if you want code, leave a comment and i'll provide

edit 3

There are a bunch of jQuery grids out there, just google it.

As for partial views something like this would work;

HTML Partial View 1 called BenefitList;

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IQueryable<Models.Benefit>>" %>

<% foreach(Benefit benefit in Model){ %>
    <% Html.RenderPartial("Benefit", benefit); %>
<% } %>

HTML Partial View 2;

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Models.Benefit>" %>

<fieldset class="Benefit benefit" id="<%= Model.Id %>">
    <legend><label class="Title"><%= Html.Encode(Model.Title) %></label> <a class="BenefitDescriptionView" href=".">View</a><a class="BenefitDescriptionEdit" href=".">Edit</a></legend>
    <div class="BenefitDescription hidden">
        <%= Html.Encode(Model.Description) %>
    </div>
</fieldset>

Some jQuery for you to do a post to an action result;

 function jQueryDeleteBenefit() {
     $.post("/Home/jQueryDeleteBenefit", { Id: YOURID },
        function(NEWHTML) {
            $('.EditProductContainer').html(NEWHTML);
        });
 }

An action result;

    [AcceptVerbs(HttpVerbs.Post)]
    public void jQueryDeleteBenefit(int id
    {
      //delete item

      return PartialView("BenefitList", AllMyBenefits);
    }
griegs
isn't the listbox a selectable list inside an expanded combobox? this isn't what im after... just want to display db records (comments) with ability to add/edit/remove etc.
Erx_VB.NExT.Coder
see my edit for more
griegs
thanks mate that was pretty much what i was looking for, nested partialviews sound like a good idea!! however, re jQuery method, which section of jQuery would i need to look into in order to read up on and understand this, is it just called 'grid' or? what would the jQuery method automate? is it worth it or better to do your own nested partialviews?
Erx_VB.NExT.Coder
see edit 3 for more details
griegs
I kinda favour the PartialView approach in these circumstances but I'm sure that's only a personal preference.
griegs
thanks so much for the detailed post, that really helps me. the $.post jquery function according to my memory is a wrapper around the $.ajax with "POST" stamped in its parameter if i am correct.on updating (adding/updating/deleting) is it best to just update the entire comments panel or is there a neat trick that can do an inline update without refreshing the entire comments panel?
Erx_VB.NExT.Coder
Choose you're own personal flavour really. But if you know the id of the deleted item then simply use jQuery to hide it. $('#itemid').hide() should do the trick nicely.
griegs
Same applied for edits in that you can change the text by using something like $('#itemid').html(yourhtml) or $('#itemid').val(yourval); val works with text boxes and html for generic containers like div's
griegs
i remember .html but what does .val do again? thanks.
Erx_VB.NExT.Coder
If you have a textbox you can get/set its value with val. $('#MyTextBox').val("New text") will put "New Text" into it. and val myStr = $('#MyTextBox').val() puts the value of the field into your variable.
griegs
thanks mate - upvoted and accepted, this should give me a good lead/start
Erx_VB.NExT.Coder