0

I'm new to MVC/Web developmnent and I'm facing a big trouble with no answer here: RadioButtonFor for a list of objects mvc

So, I'm trying to understand.

Considering I have these Model :

public class ChartsViewModel
{
    public HistoricViewModel HistoricViewModel { get;set;}
}
public class HistoricViewModel
{
    public PeriodViewModel SelectedPeriod { get; set; }
    public IEnumerable<PeriodViewModel> AllPeriodes { get; set; } 
}
public class PeriodViewModel
{
    public int Id { get; set; }
    public bool Selected { get; set; }
    public string DisplayText { get; set; }
    public string PickerFormat { get; set; }
}

ChartsViewModel is the model used in the view. Now I'm trying to understand the binding and I wrote this hard coded HTML to do that.

    <input id="HistoricViewModel_SelectedPeriod_Selected" name="HistoricViewModel.SelectedPeriod.Selected" type="hidden" value="True" />
    <input id="HistoricViewModel_SelectedPeriod_PickerFormat" name="HistoricViewModel.SelectedPeriod.PickerFormat" type="hidden" value="formatA" />
    <input id="HistoricViewModel_SelectedPeriod_Id" name="HistoricViewModel.SelectedPeriod.Id" type="radio" value="1" />
    <input id="HistoricViewModel_SelectedPeriod_DisplayText" name="HistoricViewModel.SelectedPeriod.DisplayText" type="hidden" value="pariodeA" />
    <input id="HistoricViewModel_SelectedPeriod_Selected1" name="HistoricViewModel.SelectedPeriod.Selected" type="hidden" value="False" />
    <input id="HistoricViewModel_SelectedPeriod_PickerFormat1" name="HistoricViewModel.SelectedPeriod.PickerFormat" type="hidden" value="formatB" />
    <input id="HistoricViewModel_SelectedPeriod_Id1" name="HistoricViewModel.SelectedPeriod.Id" type="radio" value="2" />
    <input id="HistoricViewModel_SelectedPeriod_DisplayText1" name="HistoricViewModel.SelectedPeriod.DisplayText" type="hidden" value="pariodeB" />

In this case only the property Id associated with the radio button is binded. How to bind the others?

Many thanks for your help.

Manta
  • 490
  • 5
  • 18
  • Never hard code you html. Always use the string typed `HtmlHelper` methods to give you 2-way model binding (plus client side validation etc). And wh do you have 2 inputs for the `Selected`, `PickerFormat`, and `DisplayText` properties of `SelectedPeriod` (only the first ones will be bound). And what is your `AllPeriodes` property for (you never use it in the view) –  Sep 17 '18 at 22:46
  • I'm just trying to understand why It doesn't work with the Html helpers. I have 2 selected because it's a radio button group. `AllPeriods` is a list of `PeriodViewModel` to create the option button group. – Manta Sep 17 '18 at 22:57
  • It does work is you do it correctly. But its not clear what you are trying to do. –  Sep 17 '18 at 22:59
  • Of course I think It should work, but at this time I have no answer to the question mentioned in the link. I've searched all the day, tried a lot of things and I must go forward... – Manta Sep 17 '18 at 23:02
  • 1
    But no where in your other question do you create form controls for `Selected` etc (which would be `@Html.CheckBoxFor(m => m.HistoricViewModel.SelectedPeriod.Selected)` etc –  Sep 17 '18 at 23:08
  • Of course, in the foreach loop. – Manta Sep 17 '18 at 23:19
  • You cannot do it in a `foreach` loop. And in any case you are binding to the `SelectedPeriod` property and its a single object, not a collection so a loop makes no sense –  Sep 17 '18 at 23:20

0 Answers0