从 IEnumerable填充 SelectListItem 时出现 NULL REF 错误
我有以下 ViewModel (TripSearchView),其中包含 IEnumerable(of AffiliateComponentTypeView) 属性,我必须使用此集合填充下拉列表。我编写了一个扩展类,它从 IEnumerable 属性填充 SelectListItem。
public class TripSearchView
{
public IEnumerable<AffiliateComponentTypeView> ComponentTypes { get; set; }
//More fields...
}
public static class DropDownHelper
{
public static IEnumerable<SelectListItem> ToSelectListItems(
this IEnumerable<AffiliateComponentTypeView> componentTypes, int selectedId)
{
return
componentTypes.Select(componentType =>
new SelectListItem
{
Text = componentType.ComponentDesc,
Value = componentType.Component_Type
});
}
}
这就是我在 aspx 中调用它的方式,但 Html.DropDownList 的第二个参数出现 NULL REFERENCE 错误。我已确保 IEnumerable
<%= Html.DropDownList(Model.TripSearch.ComponentType.ToString(), DropDownHelper.ToSelectListItems(Model.TripSearch.ComponentTypes,0))%>
在调用视图之前,我在控制器操作中使用集合填充“ComponentTypes”。
public ActionResult Index()
{
SearchView SearchView = new SearchView();
TripSearchView TripSearchView = new TripSearchView();
TripSearchView.ComponentTypes = _referenceDataService.AffiliateComponentTypes(base.GetAffiliateID());
SearchView.TripSearch = TripSearchView;
ViewData["ComponentTypesList"] = _referenceDataService.AffiliateComponentTypes(base.GetAffiliateID()).Select(componentType =>
new SelectListItem
{
Text = componentType.ComponentDesc,
Value = componentType.Component_Type
});
return View(SearchView);
}
I have following ViewModel (TripSearchView) which contains IEnumerable(of AffiliateComponentTypeView) property, I have to populate a dropdown list using this collection. I have written an extension class which populates SelectListItem from IEnumerable property.
public class TripSearchView
{
public IEnumerable<AffiliateComponentTypeView> ComponentTypes { get; set; }
//More fields...
}
public static class DropDownHelper
{
public static IEnumerable<SelectListItem> ToSelectListItems(
this IEnumerable<AffiliateComponentTypeView> componentTypes, int selectedId)
{
return
componentTypes.Select(componentType =>
new SelectListItem
{
Text = componentType.ComponentDesc,
Value = componentType.Component_Type
});
}
}
This is how I call it in my aspx but I am getting a NULL REFERENCE error for second parameter of Html.DropDownList. I have made sure that IEnumerable<AffiliateComponentTypeView> ComponentTypes
is correctly populated with values and the logic to populate the SelectListItem works correctly. Why am I getting a NULL Ref error? What am I missing?
<%= Html.DropDownList(Model.TripSearch.ComponentType.ToString(), DropDownHelper.ToSelectListItems(Model.TripSearch.ComponentTypes,0))%>
I am populating "ComponentTypes" with collection in my controller action before calling the View.
public ActionResult Index()
{
SearchView SearchView = new SearchView();
TripSearchView TripSearchView = new TripSearchView();
TripSearchView.ComponentTypes = _referenceDataService.AffiliateComponentTypes(base.GetAffiliateID());
SearchView.TripSearch = TripSearchView;
ViewData["ComponentTypesList"] = _referenceDataService.AffiliateComponentTypes(base.GetAffiliateID()).Select(componentType =>
new SelectListItem
{
Text = componentType.ComponentDesc,
Value = componentType.Component_Type
});
return View(SearchView);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我可以想到三个选项:
Model.ComponentTypes
为空(你说它不是,但我必须假设某事不是你想象的那样)我注意到您是顺便说一下,没有在方法中使用
selectedId
参数...您是故意的吗? (由于某种原因,您也没有使用您拥有扩展方法的事实......)Well, I can think of three options:
Model.ComponentTypes
is null (you say it's not, but I have to assume that something isn't the way you think it is)Model.ComponentTypes
is nullComponentDesc
orComponent_Type
I note that you're not using the
selectedId
parameter in the method, by the way... did you mean to? (You're also not using the fact that you've got an extension method, for some reason...)在您的 DropDownHelper 中,
您确定 base.GetAffiliationId 不为 null 吗?您确定服务本身不会抛出空引用异常吗?请记住,这些是 IEnumerable,因此执行可能会延迟。
In your DropDownHelper
Are you sure that base.GetAffiliationId is not null? Are you sure the service it self does not throw a null refence exception? Remember that these are IEnumerable so execution might be deffered.