Click here to Skip to main content
15,919,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written following code in my view.
<pre lang="c#">
<%Html.DropDownListFor("ddlCity",new SelectList(ViewData["City"].ToString(),"Value","Text"));%>


and following code in the controller
<pre lang="c#">

 public ActionResult Index()
        {
            DbConnect objdb = new DbConnect();
            DataTable dt= objdb.ExecuteDataset("usp_allCities");
           
            
            List<SelectListItem> cityList=new List<SelectListItem>();
            
           
            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["City"] = (IEnumerable<SelectListItem>)cityList;
                return View(ViewData);
        }


I am not able to bind the dropdown list. Can any one suggest me what can i do?
Browser gives following error -
CS0411: The type arguments for method 'System.Web.Mvc.Html.SelectExtensions.DropDownListFor<tmodel,tproperty>(System.Web.Mvc.HtmlHelper<tmodel>, System.Linq.Expressions.Expression<system.func><tmodel,tproperty>>, System.Collections.Generic.IEnumerable<system.web.mvc.selectlistitem>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
Posted
Updated 25-Nov-18 22:13pm

ViewData["City"] = new SelectList((IEnumerable<selectlistitem>)cityList,"CityId", "CityName");


@Html.DropDownList("city", ViewData["City"] as SelectList, "---Select ---")
 
Share this answer
 
try this..

HTML
@Html.DropDownList("Select", new SelectList((System.Collections.IEnumerable)ViewData["list"], "Id", "Name"))
 
Share this answer
 
Try using @Html.DropDownList instead DropDownListfor

You cannot used that Helper @Html.DropdownListFor, because the first parameters was not correct change your helper to:

@Html.DropDownList("accountid", new SelectList(ViewBag.Accounts, "AccountID", "AccountName"))

@Html.DropDownListFor receive in the first parameters a lambda expression in all overloads and is used to create strongly typed dropdowns

If your View it's strongly typed to some Model you may change your code using a helper to created a strongly typed dropdownlist, something like
@Html.DropDownListFor(x => x.accountId, new SelectList(ViewBag.Accounts, "AccountID", "AccountName"))

Here is Documentation

;_)
 
Share this answer
 
v3
Comments
Deepu S Nair 16-Mar-15 8:22am    
Instead of adding new solution Use 'Improve soulution link' to update your existing solution.
Set up your ViewData in the normal way, assigning a Key name that maps to a property in your model that will be bound on Post .

C#
ViewData["ModelPropertyName"] = new SelectList(...)

Then in your view simply add a Html.DropDownList ...

C#
@Html.DropDownList("ModelPropertyName")


Happy coding ;-)
 
Share this answer
 
Comments
Dolly Nimavat 16-Mar-15 7:48am    
It works. but now helpers are not displayed in the browser
Hardeep Saggi 16-Mar-15 7:50am    
could you paste your html helper control code for dropdownlist
Dolly Nimavat 16-Mar-15 8:05am    
<%Html.DropDownListFor("ddlCity",new SelectList(ViewData["City"].ToString(),"CityId","CityName"));%>

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