0

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?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • You can use a partial view or templates or you can implement a checkbox or a combobox for your Answer Property. Like in this example: https://stackoverflow.com/questions/46601700/pass-entire-model-with-sub-model-list-to-controller – Sebastian Siemens Nov 29 '18 at 15:00
  • @SebastianSiemens Thank you for feedback. I found these two related posts as a solution [bind array to asp-tag](https://stackoverflow.com/questions/41873171/how-can-i-bind-an-array-with-asp-for-tag) and [Post an HTML Table to ADO.NET DataTable](https://stackoverflow.com/questions/30094047/post-an-html-table-to-ado-net-datatable) – Dinorego C. Mphogo Dec 03 '18 at 14:40

0 Answers0