Click here to Skip to main content
15,116,354 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
   
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"))
}
Vishal Pand3y 9-Sep-13 9:07am
   
and which model will be used in partial view the main model in which we are saving statusID or the model for status in which i have StatusID and StatusName
Jameel VM 9-Sep-13 9:08am
   
@Html.DropDownListFor(model => model.id, (IEnumerable<SelectListItem>)ViewBag.StatusList, "--Select--", new { @class = "select" })
Jameel VM 9-Sep-13 9:09am
   
Try like this
Vishal Pand3y 9-Sep-13 9:12am
   
still getting the same error There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'StatusID
Jameel VM 9-Sep-13 9:14am
   
can you post the properties of Status class
Vishal Pand3y 9-Sep-13 9:16am
   
public class Status
{
public decimal StatusID { get; set; }
public string StatusName { get; set; }
}
Jameel VM 9-Sep-13 9:17am
   
let me do it.after that i will update you.
Jameel VM 9-Sep-13 9:49am
   
I have update my answer.Please follow that

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