将 ViewBag 转换为 List<SelectListItem> 给出空引用异常
Casting ViewBag to List<SelectListItem> gives null reference exception
下面的代码可以正常运行,但是在保存表单时,开发者工具中的网络选项卡预览中出现空引用异常
我想我明白为什么会发生异常,并且我尝试了不同的方法来针对 ViewBag 的 null 进行验证,但我总是遇到同样的错误。
请有人建议执行转换和验证的正确方法
<select asp-for="Pathways" id="pathwaysDropdown"
asp-items="((List<SelectListItem>)ViewBag.Programmes).Where(a => a.Disabled == false)">
</select>
//The below are some things I've tried but don't work, but hopefully will show what I'm trying to achieve:
asp-items="(ViewBag.Programmes ? new List<SelectListItem>() :
(List<SelectListItem>)ViewBag.Programmes).Where(a => a.Disabled == false)">
asp-items="((List<SelectListItem>)ViewBag.Programmes ??
new List<SelectListItem>()).Where(a => a.Disabled == false)">
//Error
ArgumentNullException: Value cannot be null. Parameter name: source
System.Linq.Enumerable.Where<TSource>(IEnumerable<TSource> source, Func<TSource, bool> predicate)
AspNetCore.Views_Marketing_Partials__OverviewFields.<ExecuteAsync>b__70_2() in _OverviewFields.cshtml
+
<td>
<div id="pathwaysList" class="initiallyHidden paddingbot paddingtop">
<select asp-for="Pathways" id="pathwaysDropdown"
asp-items="((List<SelectListItem>)ViewBag.Programmes).Where(a => a.Disabled == false)">
<option value="" disabled hidden>----</option>@*selected*@
</select>
<input type="hidden" asp-for="PathwaysOfProgramme" />
</div>
<table id="pathwaysTable" class="table">
<tr id="pathwayHeaders">
我认为问题是 Viewbag 没有填充到要返回的部分中。
我找到了一个更简单的解决方案,但是使用了一些 css:
select option:disabled {
display:none;
}
asp-items="ViewBag.Programmes
下面的代码可以正常运行,但是在保存表单时,开发者工具中的网络选项卡预览中出现空引用异常
我想我明白为什么会发生异常,并且我尝试了不同的方法来针对 ViewBag 的 null 进行验证,但我总是遇到同样的错误。
请有人建议执行转换和验证的正确方法
<select asp-for="Pathways" id="pathwaysDropdown"
asp-items="((List<SelectListItem>)ViewBag.Programmes).Where(a => a.Disabled == false)">
</select>
//The below are some things I've tried but don't work, but hopefully will show what I'm trying to achieve:
asp-items="(ViewBag.Programmes ? new List<SelectListItem>() :
(List<SelectListItem>)ViewBag.Programmes).Where(a => a.Disabled == false)">
asp-items="((List<SelectListItem>)ViewBag.Programmes ??
new List<SelectListItem>()).Where(a => a.Disabled == false)">
//Error
ArgumentNullException: Value cannot be null. Parameter name: source
System.Linq.Enumerable.Where<TSource>(IEnumerable<TSource> source, Func<TSource, bool> predicate)
AspNetCore.Views_Marketing_Partials__OverviewFields.<ExecuteAsync>b__70_2() in _OverviewFields.cshtml
+
<td>
<div id="pathwaysList" class="initiallyHidden paddingbot paddingtop">
<select asp-for="Pathways" id="pathwaysDropdown"
asp-items="((List<SelectListItem>)ViewBag.Programmes).Where(a => a.Disabled == false)">
<option value="" disabled hidden>----</option>@*selected*@
</select>
<input type="hidden" asp-for="PathwaysOfProgramme" />
</div>
<table id="pathwaysTable" class="table">
<tr id="pathwayHeaders">
我认为问题是 Viewbag 没有填充到要返回的部分中。
我找到了一个更简单的解决方案,但是使用了一些 css:
select option:disabled {
display:none;
}
asp-items="ViewBag.Programmes