0

Property, from which of set of hidden fields are to be populated:

public abstract class MyPageView
{        
    [UIHint("List")]
    public IEnumerable<IOption> OptionList { get; set; }
}

Command to do so:

@Html.EditorFor(m => m.OptionList)

Implementation of defined 'List' template:

@model IEnumerable<RoomWanted.FlatShare.Services.ViewModels.Options.IOption>

@for (int i = 0; i < Model.Count(); i++)
{    
    @Html.HiddenFor(x => Model.ToList()[i].Id)
    @Html.HiddenFor(x => Model.ToList()[i].Name)
}

What I GET generated:

<input id="OptionList__0__Id" name="OptionList.[0].Id" type="hidden" value="1" />
<input id="OptionList__0__Name" name="OptionList.[0].Name" type="hidden" value="New York" />

What I NEED TO GET generated, so that model binding works correctly:

<input id="OptionList_0__Id" name="OptionList[0].Id" type="hidden" value="5"></input>
<input id="OptionList_0__Name" name="OptionList[0].Name" type="hidden" value="New York"></input>

Any Ideas on how to get rid of that dot so i have OptionList[0].Id instead of OptionList.[0].Id ?

Cheers.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • I never had a reason to save collections of hidden input fields. What is your reason to do that? – Elisabeth Mar 17 '13 at 15:51
  • Apart from populating hidden input fields i am actually populating – Trebor Sadnib Mar 17 '13 at 18:15
  • Actual purpose is that i want to write an custom server-side validator for `IEnumerable OptionList` so i need the data to be bound back to the model. – Trebor Sadnib Mar 17 '13 at 18:22

1 Answers1

0

Use an IList<IOption> or IOption[] instead:

public abstract class MyPageView
{        
    [UIHint("List")]
    public IList<IOption> OptionList { get; set; }
}

and then in your view:

@model IList<RoomWanted.FlatShare.Services.ViewModels.Options.IOption>

@for (int i = 0; i < Model.Count; i++)
{    
    @Html.HiddenFor(x => x[i].Id)
    @Html.HiddenFor(x => x[i].Name)
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • this would work if i would be using it directly in view as you are showing. but i am using in my main view `@Html.EditorFor(m => m.OptionList)` (as there are more lists than just one) and those commands are using 'List' template. Maybe somebody some other way how to achieve fuctionality without loosing the flexibility of the soultion ? but thanks anyway for your post. – Trebor Sadnib Mar 19 '13 at 20:22