Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written following code in the view -
<pre lang="c#">
    <tr>
              <td>
                <%Html.Label("FromCity"); %>
               
              </td>
              <td>
                  <select id="Select1">
                      <option></option>
                 </select>             

               <%Html.DropDownList("City"); %>
               <%--<%Html.DropDownListFor("ddlCity",new SelectList(ViewData["City"].ToString(),"CityId","CityName"));%>--%>
               
              </td>
             
          </tr>

The helpers are not being displayed when I run the project, no error is displayed. All other HTML controls except helpers are displayed.
What should I do?
Posted

Replace
C#
ViewData["City"].ToString()
with
C#
ViewData["City"] as IEnumerable <SelectListItem>


XML
<%Html.DropDownListFor("ddlCity",new SelectList(ViewData["City"] as IEnumerable <SelectListItem>,"CityId","CityName"));%>
 
Share this answer
 
v3
Comments
Deepu S Nair 17-Mar-15 1:45am    
Two solution for a single question?Its better to remove one and update the other.
Hardeep Saggi 17-Mar-15 1:49am    
ok
Dolly Nimavat 17-Mar-15 12:57pm    
I solved this error by writing this code -
<% Html.DropDownList("CityName");
Now there is no error and no otput. Any html helpers are not displayed in the browser.
Please make sure you are filling viewBag.City with selectList and data
C#
ViewBag.City= new SelectList(db.Cities, "Id", "Name");

Rest of your code is correct
HTML
<%Html.DropDownList("City"); %>


;;-)
 
Share this answer
 
v2
Comments
Dolly Nimavat 16-Mar-15 12:25pm    
How does db.cities fetches data from database? I have used stored procedure to fetch the data and it returns datatable.
Following is the code I am selecting data from that datatable :
public ActionResult Index()
{
DbConnect objdb = new DbConnect();//connection to database
DataTable dt= objdb.ExecuteDataset("usp_allCities"); // call to the function that calls the stored procedure


List<SelectListItem> cityList=new List<SelectListItem>();

var mylist = new SelectList(dt.);
for (int i = 0; i < dt.Rows.Count; i++)
{
cityList.Add(new SelectListItem { Text = dt.Rows[i]["CityName"].ToString(), Value = dt.Rows[i]["CityId"].ToString() });
}

ViewData["CityName"] = (IEnumerable<SelectListItem>)cityList;
return View(ViewData);
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900