Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I do not know what is wrong in my code. following are the code classes respectively, and getting the error ArgumentNullException :

I am using the DB first approach.

Here is my model class:
XML
public partial class Employee
    {
        public int EmployeeID { get; set; }
        public Nullable<int> EmployeeTypeID { get; set; }
        public int DepartmentID { get; set; }
        public int GradeID { get; set; }

        public virtual ICollection<BillingLinkedPersonnel> BillingLinkedPersonnels { get; set; }
        public virtual Department Department { get; set; }
        public virtual EmployeeType EmployeeType { get; set; }
        public virtual ICollection<EmployeeDetail> EmployeeDetails { get; set; }
        public virtual ICollection<EmployeeEducationDetail> EmployeeEducationDetails { get; set; }
        public virtual ICollection<EmployeeLeave> EmployeeLeaves { get; set; }
        public virtual Grade Grade { get; set; }
    }


Now here is my controller class ViewBag object method:

ViewBag.EmployeeTypeName = from EmployeeType in employee.EmployeeType.Name select new SelectListItem() { Text = employee.EmployeeType.Name, Value = employee.EmployeeType.EmployeeTypeID.ToString()};


Now here is my view DropDownListFor():

@Html.DropDownListFor(m => m.EmployeeType.Name,(IEnumerable<SelectListItem>)ViewBag.EmployeeTypeName, "select city")


Can any one tell me what is wrong with this?

Thanks
Posted

see with one more condition
@if(ViewBag.EmployeeTypeName !=null){
@Html.DropDownListFor(m => m.EmployeeType.Name,(IEnumerable<SelectListItem>)ViewBag.EmployeeTypeName, "select city") 
}


make a to use ToList() for viewbag object because it needs enumerable list
                    ViewBag.EmployeeTypeName = new SelectList(employee.EmployeeType.ToList(), "EmployeeTypeID", "Name", "EmployeeTypeID");
;
 
Share this answer
 
v3
Comments
DT_2 9-May-14 6:40am    
Yes Sankarsan your solution didn`t gave me the exception, now I am rendering successful to that view, but the drop down list is not there, means that space of DropDown List Box is empty. nothing is there. No text box No dropdown list.
[no name] 9-May-14 6:49am    
@else{
@Html.DropDownList(m => m.EmployeeType.Name, new List<SelectListItem>{
new SelectListItem{Text="Select City",Value="Select City"} })
}

Put a else part to show blank dropdownlist
DT_2 9-May-14 6:52am    
Sakarsan It is not checking that if condition.
[no name] 9-May-14 6:55am    
Ok Remove @ syntax from else write like below
@if(ViewBag.EmployeeTypeName !=null){
@Html.DropDownListFor(m => m.EmployeeType.Name,(IEnumerable<SelectListItem>)ViewBag.EmployeeTypeName, "select city")
}else{
@Html.DropDownList(m => m.EmployeeType.Name, new List<SelectListItem>{
new SelectListItem{Text="Select City",Value="Select City"} })
}
DT_2 9-May-14 6:56am    
Greate, blank List is there. But what about my values, it means it is checking the if condition. I have the values in my table. so how can I check or correct or solve it?
Controller`s Create()
List<SelectListItem> employeeListItems = new List<SelectListItem>();
           foreach (var item in _entities.Employees.ToList())
           {
               employeeListItems.Add(new SelectListItem() { Text = item.FirstName + " " + item.LastName, Value = item.EmployeeID.ToString() });
           }
           ViewBag.EmployeeList = employeeListItems;



Than the in the view

@Html.DropDownListFor(model => model.EmployeeID, (IEnumerable<SelectListItem>)ViewBag.EmployeeList })



Thank you Sankarsan Sir for helping at an highest level.
 
Share this answer
 

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