Duk
Duk

Reputation: 927

how to retrieve data from database and show in dropdown in mvc

I want to retrieve data from database and show in dropdown. I used Model so i can take from model also. But it is not correctly retrieve. If In database 4 entries means it shows 4 dropdowns buttons but inside the dropdown there is no values. Can anyone help me out this problem?

My code in view

      <div class="editor-label">
    <%: Html.Label("Basic Qualification")%>
    </div>
    <div class="editor-field">

    <div id="basic-qualification-container1" style="margin-bottom:4px;" class="basic-qualification-container left">
           <% if (ViewData["BasicQualificationDegrees"]!=null){ %>
           <% for (int i=0; i < Model.CandidateQualifications.Count();i++) {%>

                        <%: Html.DropDownList("BasicQualificationDegree1", new SelectList((IEnumerable<Dial4Jobz.Models.Degree>)ViewData["CandidateBasicQualifications"], "Id", "Name", (IEnumerable<int>)ViewData["BasicQualificationDegrees"]), new { @class = "qualification" })%> 
                        <%: Html.TextBox("BasicQualificationSpecialization1", "", new { @title = "Enter basic qualification degree", @class="specialization" })%> 
                <%} %>  
            <%} else { %>
                <select id="BasicQualificationDegree1" name="BasicQualificationDegrees"></select>
            <%} %>
         </div>
         <div class="left">               
         <input type="button" id="btnAddBasicQualification" value="Add" />
         <input type="button" id="btnDelBasicQualification" value="Remove" />
         </div>
         </div>       

Upvotes: 1

Views: 858

Answers (1)

PoliDev
PoliDev

Reputation: 1458

As per your question,

 <% for (int i=0; i < Model.CandidateQualifications.Count();i++) {%>

what is this? why did you count the viewdata?

Code <% if (ViewData["BasicQualificationDegrees"]!=null){ %>

 <% var containerId = "basic-qualification-container" + i.ToString(); %>
                      <div id="<%: containerId %>" style="margin-bottom:4px;" class="basic-qualification-container left">
                          <%: Html.DropDownList("BasicQualificationDegree" + i.ToString(), new SelectList((IEnumerable<Dial4Jobz.Models.Degree>)ViewData["CandidateBasicQualifications"], "Id", "Name", basicQualifications.ElementAt(i-1).DegreeId), new { @class = "qualification" })%> 
                           <%: Html.TextBox("BasicQualificationSpecialization" + i.ToString(), basicQualifications.ElementAt(i-1).Specialization, new { @title = "Enter basic qualification degree", @class = "specialization" })%> 
                       </div>
                 <% } %>  
             <% } else { %>
                    <div id="basic-qualification-container1" style="margin-bottom:4px;" class="basic-qualification-container left">
                        <%: Html.DropDownList("BasicQualificationDegree1", new SelectList((IEnumerable<Dial4Jobz.Models.Degree>)ViewData["CandidateBasicQualifications"], "Id", "Name"), new { @class = "qualification" })%> 
                        <%: Html.TextBox("BasicQualificationSpecialization1", "", new { @title = "Enter basic qualification degree", @class = "specialization" })%> 
                    </div>

Hope It helps!.

Upvotes: 1

Related Questions