1

I have a strongly typed view as shown below:

  <div class="form-group">
      @Html.LabelFor(model => model.Text, htmlAttributes: new { @class = "control-label col-md-2" })
         <div class="col-md-10">

             @Html.DropDownListFor(model => model.Text, "Drop");    @*Error Line*@

               @Html.ValidationMessageFor(model => model.Text, "", new { @class = "text-danger" })
            </div>

With model Class DropdownCheck like below:

 public class DropdownCheck
 {
    public string Text { get; set; }
    public String Test { get; set; }
 }

Basically, I don't want to hard-code values to view through list items,but wants to bind it from database through stored procedure.

So, For that I have controller like below

   [HttpGet]
    public ActionResult Dropdown()
    {
        BusDataContext dtnew=new BusDataContext();
        ViewBag.Drop = new SelectList(dtnew.Bind_Dropdown().ToList(), "BusSrlNo", "Name");
        return View();
    }

Also storing dropdown id,text in ViewBag as above. where Bind_Dropdown() is like below

  public IEnumerable<DropdownValues> Bind_Dropdown()
   {
       var res = dt.Get_Bus_Dropdown().ToList();
       List<DropdownValues> drop = new List<DropdownValues>();

       for (var i = 0; i < res.Count; i++)
       {
           DropdownValues name = new DropdownValues();
           name.drpbusid = Convert.ToInt32(res[i].BusSrlNo);
           name.drpbusname = Convert.ToString(res[i].Name);
           drop.Add(name);
       }
       return drop;
   }

where DropdownValues is a class like below:

   public class DropdownValues
{
    public int drpbusid { get; set; }
    public string drpbusname { get; set; }
}

Stored procedure returns dropdown value and Text as BusSrlNo and Name. After doing all these stuff,getting error compilation like below: :

CS1928: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'DropDownListFor' and the best extension method overload 'System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>, System.Collections.Generic.IEnumerable)' has some invalid arguments

2 Answers 2

1

1) Change this line:

ViewBag.Drop = new SelectList(dtnew.Bind_Dropdown().ToList(), "BusSrlNo", "Name");

Into this:

ViewBag.Drop = new SelectList(dtnew.Bind_Dropdown().ToList(), "drpbusid", "drpbusname");

2)Change this line:

@Html.DropDownListFor(model => model.Text, "Drop"); 

Into this:

@Html.DropDownList("anyName", ViewBag.Drop as SelectList)

I've replicated your error on my side and those are the only two changes I had to make to get it working.I hope it helps you.

Sign up to request clarification or add additional context in comments.

Comments

0

I am not familiar with the signature you are trying to use (can .NET bind a value from a string like this?)

@Html.DropDownListFor(model => model.Text, "Drop");    @*Error Line*@

What happens if you switch this to:

@Html.DropDownListFor(m => m.Text,
 ((IEnumerable<SelectListItem>)ViewBag.Drop.Items))

1 Comment

No still I am getting-> Unable to cast object of type 'System.Collections.Generic.List1[MVC_DAL.DropdownValues]' to type 'System.Collections.Generic.IEnumerable1[System.Web.Mvc.SelectListItem]'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.