0

My view is collection of question that is rendered as textbox, dropdown and checkboxes. These question are stored in database.

For my view I have created a viewmodel

public class SpecialProgram
{
    public string Role { get; set; }

    public List<QuestionAnswer> QuestionAnswerList { get; set; }

    // other prop
}  

public class QuestionAnswer
{
    // other prop

    public int QuestionID { get; set; }

    public string Question { get; set; }

    public string ControlType { get; set; }

    public int QuestionSortOrder { get; set; }

    public string Answer { get; set; }

    public IEnumerable<SelectListItem> DataList { get; set; }
}

now here is my view

@model MyApplication.SpecialProgram

@using (Html.BeginForm("SavePage", "MyController", FormMethod.Post, new { enctype = "multipart/form-data", @id = "formData" }))
{
    @for (int i = 0; i < Model.QuestionAnswerList.Count(); i++)
    {
        // other stuff......

        else if (@Model.QuestionAnswerList[i].ControlType == "DropDown")
        {
           <div class="col-lg-12">
              <div class="input-group" style="padding: .15em .15em .15em .15em">
                 <span class="input-group-addon" style="width:350px; text-align: left;">@Model.QuestionAnswerList[i].Question</span>
                @Html.DropDownListFor(mod => mod.QuestionAnswerList[i].Answer, Model.QuestionAnswerList[i].DataList, "--Select--", new { @class = "form-control placeholder-color answers", style = "width: 150px;" })
             </div>
         </div>
      }
   }
}

Now lets say My data List is a Yes&No List. here is how i am getting my values from database and passing my values to View

public List<QuestionAnswer> MYMethod()
{
     List<QuestionAnswer> qadata = new List<QuestionAnswer>();

     List<SelectListItem> YesNoList = new List<SelectListItem>();
     YesNoList.Add( new SelectListItem { Text = "Yes", Value = "1" });
     YesNoList.Add( new SelectListItem { Text = "No", Value = "0" });         

     qa.DataList = YesNoList;

     // the qa.Answer here is 1. which is the selected dropdown value saved in db. I want yes to be pre-selected on page load. But for some reason On page load I get default value "--Select--"


}

For testing purposes only I tried setting value to dropdown. this works

var data = '@Model.QuestionAnswerList[2].Answer';
$('#ddlMYddl').val(data)

But using jquery does not really solve my problem. i want @Html.DropDownListFor to work

@Html.DropDownListFor(mod => mod.QuestionAnswerList[i].Answer, Model.QuestionAnswerList[i].DataList, "--Select--", new { @class = "form-control placeholder-color answers", style = "width: 150px;" })

to summarize. The dropdown rendered with above code has 3 select options(yes, no , --select--). Based on Answer from DB on page load ddlMYddl should have "Yes" selected but i get --Select--

Where am i going wrong

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
DotNetBeginner
  • 412
  • 2
  • 15
  • 34

1 Answers1

0

When you are in MYMethod, do you know which is selected when creating the list? If so you can use the selected property of the SelectedListItem and set it to true.

List<SelectListItem> YesNoList = new List<SelectListItem>();
YesNoList.Add( new SelectListItem { Text = "Yes", Value = "1", Selected = true });
YesNoList.Add( new SelectListItem { Text = "No", Value = "0" });  
user3064073
  • 566
  • 2
  • 6
  • 17
  • Yes i noticed that works. My question was why this @Html.DropDownListFor(mod => mod.QuestionAnswerList[i].Answer, Model.QuestionAnswerList[i].DataList,..... does not work – DotNetBeginner May 18 '17 at 17:33