0

I am using Mvc 3 and have come across the following scenario:

I've got a view model that has a list of note elemements as a property and I am using the HtmlHelper extension method EditorFor to render out the collection. All is working great. However now I need to display the note elements in the reverse order.

Is there a way to tell Mvc to reverse the elements?

// View Models
public class MyViewModel
{
    public List<Note> Notes
    {
        get;
        set;
    }
}

public class Note
{
    public string Username
    {
        get;
        set;
    }

    public DateTime ChangedDate
    {
        get;
        set;
    }

    public string Text
    {
        get;
        set;
    }
}

The elements are in oldest first order.

I can reverse the order of the elements in the collection to solve the problem which at the moments seems the most logically way to go. However that makes the javascript to insert new notes more complex as that requires the name and id of the html elements to be rewritten.

So I was hoping there maybe some sort of order option for the EditorFor method.

Another alternate would be to write a custom partial view and update the TemplateInfo.HtmlFieldPrefix but I like to keep to using the EditorTemplates if I can.

Erik Funkenbusch
  • 90,480
  • 27
  • 178
  • 274
Tom Maher
  • 1,584
  • 2
  • 19
  • 37
  • 1
    You have not shown your script, but if you add a hidden input for an `Index` property when adding new items then you don't need to worry about consecutive indexers in the `name` attribute ([example here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796)). Another option is to use the `BeginCollectionItem` helper ([example here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308)) –  Apr 09 '15 at 11:45
  • Hi @StephenMuecke thanks for help but I am looking to avoid using TemplateInfo.HtmlFieldPrefix (which is how the BeginCollectionItem method works - we've already got something similar in our code). I like the index property suggestion. Using that would allow me to control the order of the binding which is the main issue with adding new note element client side. As far as I know the elements in the collection would be bound in the order the index hidden input html elements appear in the page. The only issue with that is that I would have to manually generate the index elements. – Tom Maher Apr 09 '15 at 11:57
  • Yes, the order will be as they appear in the page so you would need to `append()` or `.perpend()` the new items as required to suit you needs, although using `EditorFor()` will not add the `Index` property so not sure what happens when you have a combination of existing items with consecutive indexers and new items with a unique `Index` property –  Apr 09 '15 at 12:03
  • I think model binders will cope with it. The advantage is I could add the new indices out of sequence, which would remove the need to rewrite the names or id's. Feel free to add your suggestion as an answer. I won't mark it as the accepted answer just yet as I will wait a few days in case someone suggests another way but thanks, the suggestion has definitely been helpful. – Tom Maher Apr 09 '15 at 12:38

0 Answers0