views:

61

answers:

1

I'm trying to do this: Editing a variable length list, ASP.NET MVC 2-style

In the post he mentions that it could be done with less code using Html.EditorFor(), but that it would be more difficult because of the indexes. Well, that's exactly what I want to do, and I don't know where to begin.

I'm an ASP.NET novice who just completed the Nerd Dinner tutorial before jumping into a project at work, so any help would be appreciated.

Update 1: Instead of generating a GUID for each item in the collection, I'd like to generate incremental indexes starting with 0. Right now the field names look like "gifts[GUID].value"; I would like them to be "gifts[0].value","gifts1.value" etc. but I don't understand how the collection keeps track and generates these indices.

A: 

Well, you begin by defining an editor template (~/Views/Shared/EditorTemplates/Gift.ascx):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyApp.Models.Gift>" %>
<div class="editorRow">
    <% using(Html.BeginCollectionItem("gifts")) { %>
        Item: <%= Html.TextBoxFor(x => x.Name) %> 
        Value: $<%= Html.TextBoxFor(x => x.Price, new { size = 4 }) %> 
    <% } %>
</div>

And then replace the RenderPartial call with EditorForModel:

<% using(Html.BeginForm()) { %>
    <div id="editorRows">
        <%= Html.EditorForModel() %>
    </div>
    <input type="submit" value="Finished" />
<% } %>

Once you've tried this you may come back and ask if you have any problems by explaining the symptoms.

Darin Dimitrov
Thanks! I've updated the summary with details about where I am now.
Mathletics