从 IEnumerable填充 SelectListItem 时出现 NULL REF 错误

发布于 2024-10-16 04:34:57 字数 1939 浏览 0 评论 0原文

我有以下 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 错误。我已确保 IEnumerableComponentTypes 正确填充了值,并且填充 SelectListItem 的逻辑工作正常。为什么我会收到 NULL Ref 错误?我缺少什么?

<%= 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

胡渣熟男 2024-10-23 04:34:58

好吧,我可以想到三个选项:

  • Model.ComponentTypes 为空(你说它不是,但我必须假设某事不是你想象的那样)
  • Model.ComponentTypes 中的组件元素之一为
  • null 组件元素之一的 ComponentDesc 或 Component_Type 为 null

我注意到您是顺便说一下,没有在方法中使用 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)
  • One of the component elements in Model.ComponentTypes is null
  • One of the component elements has a null ComponentDesc or Component_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...)

陈年往事 2024-10-23 04:34:58
<%: Html.DropDownList(
Model.ComponentType.ToString(), 
Model.TripSearch.ComponentTypes.ToSelectListItems(0))
%> 

在您的 DropDownHelper 中,

    return componentTypes.Select(
      componentType =>
         new SelectListItem 
            { Text = componentType==null?"empty":ComponentType.ComponentDesc,
              Value = componentType==null?"empty":componentType.Component_Type
            })

您确定 base.GetAffiliationId 不为 null 吗?您确定服务本身不会抛出空引用异常吗?请记住,这些是 IEnumerable,因此执行可能会延迟。

<%: Html.DropDownList(
Model.ComponentType.ToString(), 
Model.TripSearch.ComponentTypes.ToSelectListItems(0))
%> 

In your DropDownHelper

    return componentTypes.Select(
      componentType =>
         new SelectListItem 
            { Text = componentType==null?"empty":ComponentType.ComponentDesc,
              Value = componentType==null?"empty":componentType.Component_Type
            })

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文