MVC post a list of complex objects

前端 未结 5 1894
眼角桃花
眼角桃花 2020-11-30 06:41

I have a FeedbackViewModel that contains a list of questions:

public class FeedbackViewModel
{
    public List Questions { get; set;         


        
5条回答
  •  粉色の甜心
    2020-11-30 07:32

    Using Razor you can implement the for loop using a dictionary as follows without making changes to your object:

    @foreach (var x in Model.Questions.Select((value,i)=>new { i, value }))
    {
         if (Model.Questions[x.i].QuestionType == "Single")
         {
              @Html.EditorFor(modelItem => (modelItem.Questions[x.i] as OpenDataPortal.ViewModels.SingleQuestionViewModel).AnswerText)
         }
       ...
    }
    

    The collection needs to be either a List or Array for this to work.

提交回复
热议问题