Answered by:
Html.DropDownListFor with SelectList method on update

Question
-
User-164452226 posted
Good day Tech Gurus
l have a module in my MVC app called DepartmentSubject, where l will be allocating SubjectID to a Department. The model is as follows:
public class DepartmentSubject { [Key] public int SubjectID { get; set; } public int DepSubID { get; set; } public int DepartmentID { get; set; } public Int64 AdminUserID { get; set; } [NotMapped] public List<Subject> SubjectList { get; set; } [NotMapped] public List<Department> DepartmentList { get; set; } }
The following is a portion of a view which selects SubjectName and DepartmentName but will be saved as SubjectID and DepartmentID respectively:
<div class="form-group"> @Html.LabelFor(model => model.DepartmentID, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownListFor(Model => Model.DepartmentID, new SelectList(Model.DepartmentList, "DepartmentID", "Department"), "Select Department") @Html.ValidationMessageFor(model => model.DepartmentID, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.SubjectID, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownListFor(model => model.SubjectID, new SelectList(Model.SubjectList, "SubjectID", "Subject"), "Select Subject") @Html.ValidationMessageFor(model => model.SubjectID, "", new { @class = "text-danger" }) </div> </div>
When l am inserting a record for the first time, the view initializes without any problem and l can select DepartmentID and SubjectID without any issue. The issue is when l try to update the record. The Update method contains a method to query the database using DepSubID, a unique ID which pulls a specific DepartmentSubject record(return View(this.objDepartmentSubject.GetbyID(parameters))). The DropDownListFor with SelectList methods cannot be populated. The following error is generated:
System.ArgumentNullException: 'Value cannot be null. Parameter name: items'
What can l do so that l can be able to update DepartmentSubject model?
Friday, December 7, 2018 1:53 PM
Answers
-
User-271186128 posted
Hi tmatiwure,
This is my model:
public class DepartmentSubject { [Key] public int DepSubID { get; set; } public int SubjectID { get; set; } public int DepartmentID { get; set; } public Int64 AdminUserID { get; set; } [NotMapped] public List<Subject> SubjectList { get; set; } [NotMapped] public List<Department> DepartmentList { get; set; } }
Yes, I know that. Since the SubjectList and DepartmentList are the List Type, so, in the controller action method, we need to assign an object list to them, instead of string type.
So I suggest you check the action method, and make sure the data is an object List, like this:
DepartmentSubject ds = new DepartmentSubject() { DepSubID = 101 }; //using a list object to set the value. ds.SubjectList = new List<Subject>() { new Subject(){ SubjectID=1001, Subject ="S1"}, new Subject(){ SubjectID=1002, Subject ="S2"}, new Subject(){ SubjectID=1003, Subject ="S3"} }; ds.DepartmentList = new List<Department>() { new Department(){ DepartmentID=1, Department="D1" }, new Department(){ DepartmentID=2, Department="D2" }, new Department(){ DepartmentID=3, Department="D3" } }; return View(ds);
Here are some related articles about binding dropwnlist in asp.net MVC, you could refer to them:
Best regards,
Dillion- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, December 12, 2018 1:56 AM
All replies
-
User-271186128 posted
Hi tmatiwure,
When l am inserting a record for the first time, the view initializes without any problem and l can select DepartmentID and SubjectID without any issue. The issue is when l try to update the record. The Update method contains a method to query the database using DepSubID, a unique ID which pulls a specific DepartmentSubject record(return View(this.objDepartmentSubject.GetbyID(parameters))). The DropDownListFor with SelectList methods cannot be populated. The following error is generated:Please check your code in the Edit method, after getting the specific DepartmentSunject, you should also set values for the SubjectList and DepartmentList properties.
//get the specific DepartmentSubject
DepartmentSubject dep = this.objDepartmentSubject.GetbyID(parameters);
//get the SubjectList, and set value. dep.SubjectList = XX;
//Get the DepartmentList, and set value. dep.DepartmentList = XX ; return View(dep);Best regards,
DillionMonday, December 10, 2018 5:23 AM -
User-164452226 posted
Hi tmatiwure,
tmatiwure
When l am inserting a record for the first time, the view initializes without any problem and l can select DepartmentID and SubjectID without any issue. The issue is when l try to update the record. The Update method contains a method to query the database using DepSubID, a unique ID which pulls a specific DepartmentSubject record(return View(this.objDepartmentSubject.GetbyID(parameters))). The DropDownListFor with SelectList methods cannot be populated. The following error is generated:Please check your code in the Edit method, after getting the specific DepartmentSunject, you should also set values for the SubjectList and DepartmentList properties.
//get the specific DepartmentSubject DepartmentSubject dep = this.objDepartmentSubject.GetbyID(parameters); //get the SubjectList, and set value. dep.SubjectList = XX; //Get the DepartmentList, and set value. dep.DepartmentList = XX ; return View(dep);
Best regards,
DillionAs you advised:
public ActionResult UpdateDepartmentSubject(int DepSubID) { object[] parameters = { DepSubID }; var depSub = objDepartmentSubject.GetbyID(parameters); if (depSub != null) { //Fetching the DepartmentID and SubjectID associated with DepSubID Session["depId"] = depSub.DepartmentID.ToString(); Session["subId"] = depSub.SubjectID.ToString(); } Int64 depId = Convert.ToInt64(HttpContext.Session["depId"]); Int64 subId = Convert.ToInt64(HttpContext.Session["subId"]); object[] subjects = { subId }; object[] departments = { depId }; var dep = objDepartment.GetbyID(departments); if (dep != null) { //Use the DepartmentID to get the DepartmentName Session["departmentName"] = dep.DepartmentName.ToString(); } var sub = objSubject.GetbyID(subjects); if (sub != null) { //Use the SubjectID to get the SubjectName Session["subjectName"] = sub.SubjectName.ToString(); } string subName = (string)Session["subjectName"]; string depName = (string)Session["departmentName"]; DepartmentSubject departmentSubject = this.objDepartmentSubject.GetbyID(parameters); //Assign the SubjectName to SubjectList departmentSubject.SubjectList = subName;//Error-can not implicitly convert type 'string' to 'System.Collections.Generic.List<AppName.Entities.Subject>' //Assign the DepartmentName to DepartmentList departmentSubject.DepartmentList = depName;//Error-can not implicitly convert type 'string' to 'System.Collections.Generic.List<AppName.Entities.Department>' return View(departmentSubject); }
But l am getting the errors that l have highlighted even before running the application, actually the subName and depName are highlighted in red.
departmentSubject.SubjectList = subName;//Error-can not implicitly convert type 'string' to 'System.Collections.Generic.List<AppName.Entities.Subject>'
//Assign the DepartmentName to DepartmentList
departmentSubject.DepartmentList = depName;//Error-can not implicitly convert type 'string' to 'System.Collections.Generic.List<AppName.Entities.Department>'Monday, December 10, 2018 2:59 PM -
User-271186128 posted
Hi tmatiwure,
tmatiwure
DepartmentSubject departmentSubject = this.objDepartmentSubject.GetbyID(parameters); //Assign the SubjectName to SubjectList departmentSubject.SubjectList = subName;//Error-can not implicitly convert type 'string' to 'System.Collections.Generic.List<AppName.Entities.Subject>' //Assign the DepartmentName to DepartmentList departmentSubject.DepartmentList = depName;//Error-can not implicitly convert type 'string' to 'System.Collections.Generic.List<AppName.Entities.Department>'
Please check the DepartmentSubject model, the SubjectList and DepartmentList are the List type, instead of the string type. Try to refer the following code to set the value.
DepartmentSubject ds = new DepartmentSubject() { DepSubID = 101 }; //using a list object to set the value. ds.SubjectList = new List<Subject>() { new Subject(){ SubjectID=1001, Subject ="S1"}, new Subject(){ SubjectID=1002, Subject ="S2"}, new Subject(){ SubjectID=1003, Subject ="S3"} }; ds.DepartmentList = new List<Department>() { new Department(){ DepartmentID=1, Department="D1" }, new Department(){ DepartmentID=2, Department="D2" }, new Department(){ DepartmentID=3, Department="D3" } }; return View(ds);
Best regards,
DillionTuesday, December 11, 2018 1:44 AM -
User-164452226 posted
Hi tmatiwure,
tmatiwure
DepartmentSubject departmentSubject = this.objDepartmentSubject.GetbyID(parameters); //Assign the SubjectName to SubjectList departmentSubject.SubjectList = subName;//Error-can not implicitly convert type 'string' to 'System.Collections.Generic.List<AppName.Entities.Subject>' //Assign the DepartmentName to DepartmentList departmentSubject.DepartmentList = depName;//Error-can not implicitly convert type 'string' to 'System.Collections.Generic.List<AppName.Entities.Department>'
Please check the DepartmentSubject model, the SubjectList and DepartmentList are the List type, instead of the string type. Try to refer the following code to set the value.
DepartmentSubject ds = new DepartmentSubject() { DepSubID = 101 }; //using a list object to set the value. ds.SubjectList = new List<Subject>() { new Subject(){ SubjectID=1001, Subject ="S1"}, new Subject(){ SubjectID=1002, Subject ="S2"}, new Subject(){ SubjectID=1003, Subject ="S3"} }; ds.DepartmentList = new List<Department>() { new Department(){ DepartmentID=1, Department="D1" }, new Department(){ DepartmentID=2, Department="D2" }, new Department(){ DepartmentID=3, Department="D3" } }; return View(ds);
Best regards,
DillionThis is my model:
public class DepartmentSubject { [Key] public int DepSubID { get; set; } public int SubjectID { get; set; } public int DepartmentID { get; set; } public Int64 AdminUserID { get; set; } [NotMapped] public List<Subject> SubjectList { get; set; } [NotMapped] public List<Department> DepartmentList { get; set; } }
Tuesday, December 11, 2018 12:52 PM -
User-474980206 posted
Your issue is simple. The two lists are null in the post back model. The post back action needs to recreate them if its view requires them.
Note: only model data mapped to a form element is included in the post back. Selects only post the selected values.Tuesday, December 11, 2018 3:21 PM -
User503812343 posted
public ActionResult UpdateDepartmentSubject(int DepSubID) { object[] parameters = { DepSubID }; var depSub = objDepartmentSubject.GetbyID(parameters); if (depSub != null) { //Fetching the DepartmentID and SubjectID associated with DepSubID Session["depId"] = depSub.DepartmentID.ToString(); Session["subId"] = depSub.SubjectID.ToString(); } Int64 depId = Convert.ToInt64(HttpContext.Session["depId"]); Int64 subId = Convert.ToInt64(HttpContext.Session["subId"]); object[] subjects = { subId }; object[] departments = { depId }; var dep = objDepartment.GetbyID(departments); if (dep != null) { //Use the DepartmentID to get the DepartmentName Session["departmentName"] = dep.DepartmentName.ToString(); } var sub = objSubject.GetbyID(subjects); if (sub != null) { //Use the SubjectID to get the SubjectName Session["subjectName"] = sub.SubjectName.ToString(); } string subName = (string)Session["subjectName"]; string depName = (string)Session["departmentName"]; DepartmentSubject departmentSubject = this.objDepartmentSubject.GetbyID(parameters);
//Assign the SubjectName to SubjectList departmentSubject.SubjectList = new SubjectList ();
departmentSubject.SubjectList.add(subName); //Assign the DepartmentName to DepartmentList departmentSubject.DepartmentList = new DepartmentList();
departmentSubject.DepartmentList.add(depName);
return View(departmentSubject); }Try updated code highlighted in yellow
Tuesday, December 11, 2018 9:50 PM -
User-271186128 posted
Hi tmatiwure,
This is my model:
public class DepartmentSubject { [Key] public int DepSubID { get; set; } public int SubjectID { get; set; } public int DepartmentID { get; set; } public Int64 AdminUserID { get; set; } [NotMapped] public List<Subject> SubjectList { get; set; } [NotMapped] public List<Department> DepartmentList { get; set; } }
Yes, I know that. Since the SubjectList and DepartmentList are the List Type, so, in the controller action method, we need to assign an object list to them, instead of string type.
So I suggest you check the action method, and make sure the data is an object List, like this:
DepartmentSubject ds = new DepartmentSubject() { DepSubID = 101 }; //using a list object to set the value. ds.SubjectList = new List<Subject>() { new Subject(){ SubjectID=1001, Subject ="S1"}, new Subject(){ SubjectID=1002, Subject ="S2"}, new Subject(){ SubjectID=1003, Subject ="S3"} }; ds.DepartmentList = new List<Department>() { new Department(){ DepartmentID=1, Department="D1" }, new Department(){ DepartmentID=2, Department="D2" }, new Department(){ DepartmentID=3, Department="D3" } }; return View(ds);
Here are some related articles about binding dropwnlist in asp.net MVC, you could refer to them:
Best regards,
Dillion- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, December 12, 2018 1:56 AM -
User-164452226 posted
Hi tmatiwure,
tmatiwure
This is my model:
public class DepartmentSubject { [Key] public int DepSubID { get; set; } public int SubjectID { get; set; } public int DepartmentID { get; set; } public Int64 AdminUserID { get; set; } [NotMapped] public List<Subject> SubjectList { get; set; } [NotMapped] public List<Department> DepartmentList { get; set; } }
Yes, I know that. Since the SubjectList and DepartmentList are the List Type, so, in the controller action method, we need to assign an object list to them, instead of string type.
So I suggest you check the action method, and make sure the data is an object List, like this:
DepartmentSubject ds = new DepartmentSubject() { DepSubID = 101 }; //using a list object to set the value. ds.SubjectList = new List<Subject>() { new Subject(){ SubjectID=1001, Subject ="S1"}, new Subject(){ SubjectID=1002, Subject ="S2"}, new Subject(){ SubjectID=1003, Subject ="S3"} }; ds.DepartmentList = new List<Department>() { new Department(){ DepartmentID=1, Department="D1" }, new Department(){ DepartmentID=2, Department="D2" }, new Department(){ DepartmentID=3, Department="D3" } }; return View(ds);
Here are some related articles about binding dropwnlist in asp.net MVC, you could refer to them:
Best regards,
DillionYour explanation and the links you provided nailed it for me. Thanks
Saturday, December 15, 2018 1:56 PM