I have a MVC Razor view intended for HttpPost submit to a controller. The view has a class StatementGroupViewModel as its ModelView. The ModelView class has a collection as a property of type List<StatementViewModel>
, these collection property is used to store StatementViewModel objects that the razer view needs to display. Also inputs for each of the object in the collection needs to be collected. Please see below for an example and my question(at the end).
@model Models.StatementGroupViewModel
@Html.BeginForm("StatementGroup", "Controller", FormMethod.Post){
@foreach(StatementGroupViewModel stateview in Model.statementList){
// each StatementViewModel properties displayed here
<input type="hidden" asp-for="stateview.Answer" />
}
}
StatementGroupViewModel class as a ViewModel for our view above.
public class StatementGroupViewModel
{
public List<StatementViewModel> statementsList { get; set; }
}
StatementViewModel class as a ViewModel for other views.
public class StatementViewModel
{
public int Answer { get; set; }
}
My question is as follows. How can I use html <input>
tag and asp-for="stateview.Answer"
dynamically to set the property Answer
for each of the StatementviewModel object in the StatementGroupViewModel connection?