Hi Friends ,
I am stuck into the situation known as
(Index was out of range. Must be non-negative and less than the size of the collection.) for Html Dropdownlist
This Exception raises when i post a request in asp.net mvc to an action method it keeps on giving me above error
View
@using PagedList;
@using PagedList.Mvc;
@model IPagedList
@Html.DropDownList("Id", new SelectList((List)ViewBag.Id, "Value", "Text"), "Select")
Controller
[Authorize]
[HttpPost]
public ActionResult Base(Prices prices, int? page)
{
prices.commodity = ListCommodities(2);
ViewBag.Id = prices.commodity;
List lstPrices = new List();
lstPrices
var lstPrices1 = lstPrices.ToPagedList(page ?? 1, 10);
return View(lstPrices);
}
//Method to get Commodities
public List ListCommodities(int id)
{
Prices prices = new Prices();
List lstCommodities = new List();
DataTable dt = DAL.PricesDAL.ListCommodities(id);
if (dt != null)
{
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
lstCommodities.Add(new SelectListItem() { Value = dr["Id"].ToString(), Text = dr["Product"].ToString() });
}
}
}
return lstCommodities;
}
2 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.

ramya bharathPosted Jun 5, 2015, 9:32 AM
Lucky LaxmanPosted Jun 5, 2015, 9:31 AM
foreach (var item in DaL.yourExistedQuery().ToList())
{
SelectListItem setitem = new SelectListItem()
{
Value = item.ID.ToString(),
Text = item.Name
};
lstCommodities .Add(setitem);
}
ViewBag.DDL = lstCommodities ;