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