Answered by:
Razor Listbox help

Question
-
User-507786106 posted
I have a listbox I my code below, I need to add the model Values StudentList is empty, how do I check for empty listbox?
Problem: Null StudentList fails or causes null error. My code could be constructed incorrectly....
ViewBag.StudentList <--- has 10 records
List<MessageCenterModel> objData = new List<MessageCenterModel>
foreach (var item in ViewBag.StudentList)
{
MessageCenterModel obj = new MessageCenterModel();
objData.StudentList.Add <---- I get lost here -- How can I add the values and text here
}
Model Code has
public List<SelectListItem> StudentList {get; set;}
Thursday, June 20, 2019 1:00 PM
Answers
-
User475983607 posted
One issue is the ViewBag must be cast to the proper type.
foreach (var item in (List<SelectListItem>)ViewBag.StudentList )
The other issue is, you are trying to add a SelectListItem type to a List<MessageCenterModel>.
Can you explain what you are trying to do here? It seems you could just use an Html helper to populate a listbox.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, June 20, 2019 1:24 PM -
User-1038772411 posted
Hello, Slimbunny
Simply you can use below code for add model data into selectedListitem.
var lst1 = new List<MessageCenterModel>(); lst1.Add(new MessageCenterModel{ Text1= "text1data", Text2= "text2data" });
//Checking for Model is null or empty
if(lst1 != null && lst1!=""){
foreach (var item in lst1)
{
StudentList.Add(new SelectListItem() { Value = item.valuename, Text = item.textname });
}
}public List<SelectListItem> StudentList { get; } = new List<SelectListItem>();
Thanks.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, June 20, 2019 1:33 PM -
User665608656 posted
Hi slimbunny,
According to your description,I'm not sure about the relationship between objects in your code.
First, you should judge ViewBag.StudentList is not null,then loop it.
Next,you could try to write this on the basis of your code:
List<SelectListItem> listSelectListItems = new List<SelectListItem>();
foreach (var item in ViewBag.StudentList) { SelectListItem selectList = new SelectListItem() { Text = item.Name, Value = item.ID.ToString(), Selected = item.IsSelected }; listSelectListItems.Add(selectList); }Here's an example of listbox binding data in mvc that you could refer to:
https://www.codeproject.com/articles/771999/asp-net-mvc-how-to-create-a-listbox
Best Regards,
YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, June 21, 2019 8:29 AM
All replies
-
User475983607 posted
One issue is the ViewBag must be cast to the proper type.
foreach (var item in (List<SelectListItem>)ViewBag.StudentList )
The other issue is, you are trying to add a SelectListItem type to a List<MessageCenterModel>.
Can you explain what you are trying to do here? It seems you could just use an Html helper to populate a listbox.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, June 20, 2019 1:24 PM -
User-1038772411 posted
Hello, Slimbunny
Simply you can use below code for add model data into selectedListitem.
var lst1 = new List<MessageCenterModel>(); lst1.Add(new MessageCenterModel{ Text1= "text1data", Text2= "text2data" });
//Checking for Model is null or empty
if(lst1 != null && lst1!=""){
foreach (var item in lst1)
{
StudentList.Add(new SelectListItem() { Value = item.valuename, Text = item.textname });
}
}public List<SelectListItem> StudentList { get; } = new List<SelectListItem>();
Thanks.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, June 20, 2019 1:33 PM -
User665608656 posted
Hi slimbunny,
According to your description,I'm not sure about the relationship between objects in your code.
First, you should judge ViewBag.StudentList is not null,then loop it.
Next,you could try to write this on the basis of your code:
List<SelectListItem> listSelectListItems = new List<SelectListItem>();
foreach (var item in ViewBag.StudentList) { SelectListItem selectList = new SelectListItem() { Text = item.Name, Value = item.ID.ToString(), Selected = item.IsSelected }; listSelectListItems.Add(selectList); }Here's an example of listbox binding data in mvc that you could refer to:
https://www.codeproject.com/articles/771999/asp-net-mvc-how-to-create-a-listbox
Best Regards,
YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, June 21, 2019 8:29 AM