Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i am trying to bind a dropdown using partial view my code is
For partial View
C#
@model TenderKhabarCRM.Models.Status

{ 
    @Html.DropDownList("status")
    
}

in view
C#
<tr>
                <td>
                    Status
                </td>
                <td>
                    :
                </td>
                <td>
                    <div id="Status">@Html.Partial("StatusUserControl", Model)</div>
                </td>
            </tr>

in controller I have
C#
 public ActionResult Index()
        {
            List<TenderKhabarCRM.Models.Status> status = new List<TenderKhabarCRM.Models.Status>();
            status = TenderKhabarCRM.DAL.DAL.GetAllStatus();
            ViewBag.StatusList = new SelectList(status, "StatusID", "StatusName");
 return PartialView(status);
        }

public static List<Status> GetAllStatus()
        {
            SqlDataReader reader = null;
            SqlParameter[] sqlParameter = new SqlParameter[] { };
            List<Status> statusList = new List<Status>();
            try
            {
                reader = DalSqlHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, "spGetAllStatus", sqlParameter);

                while (reader.Read())
                {
                    TenderKhabarCRM.Models.Status status = MapStatusObject(reader);
                    statusList.Add(status);
                }
            }
            finally
            {
                if (reader != null && !reader.IsClosed)
                {
                    reader.Close();
                    reader = null;
                }
            }
            return statusList;
        }

but in partial view an error comes There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'status'. i just can't understand what it is can any one help.....
Posted
Comments
Jameel VM 9-Sep-13 7:14am    
in which view you render the partial view?
Vishal Pand3y 9-Sep-13 7:17am    
here <div id="Status">@Html.Partial("StatusUserControl", Model)</div>
Vishal Pand3y 9-Sep-13 7:38am    
can u help abit bcs i am stuck in it from 1 week
Vishal Pand3y 9-Sep-13 8:34am    
in Controller i have changed from viewbag to
this.ViewData["StatusList"] = new SelectList(status, "StatusID", "StatusName");
but still get the same error

I have created an sample which works fine.Please try like that
C#
public class TestController : Controller
    {
        //
        // GET: /Test/

        public ActionResult Index()
        {

            ViewBag.StatusList =
                           DropDownList<Status>.LoadItems(GetData(), "StatusId", "Name");
            return View();
        }
        public ActionResult _PartialView()
        {
            return PartialView();
        }
       public List<Status>  GetData()
       {
           var list = new List<Status> {new Status() {StatusId = 1, Name = "AAA"}};
           return list;
       }
    }
    public class  Status
    {
        public decimal StatusId { get; set; }
        public string Name { get; set; }
    }
    public class MyViewModel
    {
        public int StatusId { get; set; }
    }
    public static class DropDownList<T>
    {
        public static SelectList LoadItems(IList<T> collection, string value, string text)
        {
            return new SelectList(collection, value, text);
        }
    }


Partial View code
C#
@model MvcApplication1.Controllers.MyViewModel

 @Html.DropDownListFor(model => model.StatusId, (IEnumerable<SelectListItem>)ViewBag.StatusList, "--Select--", new { @class = "select" })


Index View
C#
@model MvcApplication1.Controllers.MyViewModel
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
@Html.Partial("_PartialView")


Note: Please make sure that partial view should be inside of View>>Test>> folder
Hope this helps
 
Share this answer
 
v2
Comments
Vishal Pand3y 9-Sep-13 8:13am    
still getting same error in @model TenderKhabarCRM.Models.Status
{
@Html.DropDownList("status")

}
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'status'.
Vishal Pand3y 9-Sep-13 8:16am    
in partial view basically
Jameel VM 9-Sep-13 9:00am    
that is because of the data loading to dropdown
Jameel VM 9-Sep-13 9:01am    
can you post the partial view code
Vishal Pand3y 9-Sep-13 9:04am    
@model TenderKhabarCRM.Models.Status
{
@Html.DropDownList("StatusList"))
}

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