Question

I have the following View. This is the whole code of my view

<asp:Content ID="Content2" ContentPlaceHolderID="HeadContent" runat="server">
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
</asp:Content>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    <select id="clientDDL">
                    </select>
                    <% Html.DropDownList("clientDDL",
                        new SelectList((IEnumerable)ViewData["Clients"], "Code", "Name")); %>
                </td>
            </tr>
        </table>
    </div>
</asp:Content>

I use the next ViewData from my Model:

public static List<lookup> GetClients()
        {
            using (ModelDataContext data = new ModelDataContext(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
            {
                List<lookup> retVal = new List<lookup>();
                List<Client> lClient = data.Clients.Where(a => a.IsActive == true).ToList();
                lClient = lClient.OrderBy(o => o.ClientName).ToList();

                foreach (Client item in lClient)
                {
                    retVal.Add(new lookup() { Code = item.ClientID.ToString(), Name = item.ClientName, });
                }
                return retVal;
            }            
        }

This is my Controller:

public ActionResult ProcedureFilter()
        {
            ViewData["Clients"] = WorkflowModels.GetClients();
            return View();
        }

What did I miss? My DropDownList is still empty. Maybe i have to put some reference? Any suggestions please.

UPDATE: The problem was ';' at the end of part <%= %>. The works code:

<%= Html.DropDownList("clientDDL",
                            new SelectList((IEnumerable)ViewData["Clients"], "Code", "Name")) %>
Was it helpful?

Solution

In the ASP.NET code you provided, 2 HTML select tags (or DropdownLists) are defined with the same id (cliendDDL) :

   <select id="clientDDL"> <-- the first one
   </select> 
   <% Html.DropDownList("clientDDL",
      new SelectList((IEnumerable)ViewData["Clients"], "Code", "Name")); %> <-- the second one, Html.DropDownList generating a select under the hood.

Would it be possible that only the first one is displayed (the empty one) and the second remains hidden because of a missing character, changing your expected HTML generation routine (Html.DropDownList) into an Embedded Code Block?

Maybe instead of :

<% Html.DropDownList

it should be :

<%= Html.DropDownList or <%: Html.DropDownList

See here for <%:%> ASP.NET 4 syntax

Edited

Also the ";" end of line character is not needed and should be removed.

OTHER TIPS

View Page: "<%=Html.DropDownList("Description", New SelectList(ViewData("Description")), "--Select--")%>"

Contoller:

Function Purchase() As ActionResult:

    Dim dataContext As New tblMVCDataContext
    Dim teststr As String
    Dim itemVar = From U In dataContext.Items Select U.Description

    For Each tbItem In itemVar

        teststr += tbItem + ","
    Next
    ViewData("Description") = teststr.Split(",")
    Return View(itemVar)
End Function
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top