1

我想从数据库中检索数据并显示在下拉列表中。我使用了模型,所以我也可以从模型中获取。但它没有正确检索。如果在数据库中 4 个条目意味着它显示 4 个下拉按钮,但在下拉列表中没有值。谁能帮我解决这个问题?

我的代码在视图中

      <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>       
4

1 回答 1

1

根据你的问题,

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

这是什么?你为什么要计算viewdata?

代码 <% 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>

希望能帮助到你!。

于 2013-03-26T07:28:56.857 回答