问题
I have a Model :
public class Period
{
public int Id { get; set; }
public bool Selected { get; set; }
public string DisplayText { get; set; }
public string PickerFormat { get; set; }
}
and the ViewModel (just involved part):
public class ChartsViewModel
{
public HistoricViewModel HistoricViewModel { get;set;}
}
public class HistoricViewModel
{
public Period SelectedPeriod { get; set; }
public IEnumerable<Period> AllPeriodes { get; set; }
}
In my view, I want to display a list of radio buttons to get the selected Period
in my Model.
That's what I'm doing :
@foreach (Period period in Model.HistoricViewModel.AllPeriodes)
{
@Html.RadioButtonFor(m => m.HistoricViewModel.SelectedPeriod.Id, period.Id)
@period.DisplayText
}
At this step, SelectedPeriod.Id
is well binded to ChartsViewModel
.
Now, how to bind other properties (Selected
,PickerFormat
and DisplayText
)?
Thanks
EDIT: Full View
@{
ViewBag.Title = "Charts Page";
}
@model MultiRetouchesService.ChartsViewModel
@using MultiRetouchesService;
@Scripts.Render("~/bundles/charts")
<script type="text/javascript">
$(document).ready(function () {
//DropdownList
setAllDropdowns();
//DateTimePicker
setDateTimePickers();
});
</script>
<div class="container-fluid">
<div class="row">
<div class="col-lg-2 col-md-3">
@using (Html.BeginForm(null, null, new { @id = string.Empty }, FormMethod.Post, new { @id = "FilterForm", onchange = "DisplayChart()" }))
{
<div class="d-flex flex-sm-nowrap flex-md-wrap flex-wrap justify-content-center">
<div class="col-md-12 col-lg-12 col-sm-6 bg-light border border-primary rounded text-dark text-center mt-1">
<h5>Dates</h5>
<div class="form-group">
<div class="input-group date" id="startingDate">
<span class="align-middle my-auto pr-2">Du:</span>
@Html.TextBoxFor(m => m.HistoricViewModel.StartingDate, new { @class = "form-control mt-1" })
<div class="input-group-addon input-group-append">
<span class="input-group-text fa fa-calendar mt-1"></span>
</div>
</div>
</div>
<div class="form-group">
<div class="input-group date" id="endingDate">
<span class="align-middle my-auto pr-2">Au:</span>
@Html.TextBoxFor(m => m.HistoricViewModel.EndingDate, new { @class = "form-control mt-1" })
<div class="input-group-addon input-group-append">
<span class="input-group-text fa fa-calendar mt-1"></span>
</div>
</div>
</div>
</div>
<div class="bg-light border border-primary rounded text-center text-dark mt-1 ml-sm-1 ml-lg-0 ml-md-0">
<h5 class="text-center">Période</h5>
@foreach (PeriodViewModel period in Model.HistoricViewModel.AllPeriodes)
{
@Html.RadioButtonFor(m => m.HistoricViewModel.SelectedPeriod.Id, period.Id)
@Html.LabelFor(m => period.PickerFormat)
}
</div>
</div>
<div class="border border-primary rounded text-dark text-center bg-light mt-1">
<h5 class="text-center">Filtres</h5>
<div class="d-flex flex-wrap justify-content-center">
<div class="m-1">@Html.DropDownListFor(m => m.HistoricViewModel.SelectedVehicules, Model.HistoricViewModel.AllVehicules, new { id = "dropDownVehicules", multiple = "multiple" })</div>
<div class="m-1">@Html.DropDownListFor(m => m.HistoricViewModel.SelectedSectors, Model.HistoricViewModel.AllSectors, new { id = "dropDownSectors", multiple = "multiple" })</div>
<div class="m-1">@Html.DropDownListFor(m => m.HistoricViewModel.SelectedImputations, Model.HistoricViewModel.AllImputations, new { id = "dropDownImputations", multiple = "multiple" })</div>
<div class="m-1">@Html.DropDownListFor(m => m.HistoricViewModel.SelectedMissions, Model.HistoricViewModel.AllMissions, new { id = "dropDownMissions", multiple = "multiple" })</div>
<div class="m-1">@Html.DropDownListFor(m => m.HistoricViewModel.SelectedPieces, Model.HistoricViewModel.AllPieces, new { id = "dropDownPieces", multiple = "multiple" })</div>
<div class="m-1">@Html.DropDownListFor(m => m.HistoricViewModel.SelectedDefaults, Model.HistoricViewModel.AllDefaults, new { id = "dropDownDefaults", multiple = "multiple" })</div>
<div class="m-1">@Html.DropDownListFor(m => m.HistoricViewModel.SelectedLocalizations, Model.HistoricViewModel.AllLocalizations, new { id = "dropDownLocalizations", multiple = "multiple" })</div>
</div>
</div>
}
</div>
<div class="col-lg-10 col-md-9">
<canvas id="barChart" width="400" height="150"></canvas>
</div>
</div>
</div>
回答1:
Simply continue the code of your foreach:
@foreach (Period period in Model.HistoricViewModel.AllPeriodes)
{
Html.RadioButtonFor(m => m.HistoricViewModel.SelectedPeriod.Id, period.Id);
Html.LabelFor(m => m.HistoricViewModel.SelectedPeriod.DisplayText);
}
来源:https://stackoverflow.com/questions/52370239/radiobuttonfor-for-a-list-of-objects-mvc