简体   繁体   English

ASP.NET Core MVC:为什么可选列表参数从不 null?

[英]ASP.NET Core MVC: Why is the optional list parameter never null?

I have an MVC Application and I've got an Index method in my Controller.我有一个 MVC 应用程序,并且在我的 Controller 中有一个索引方法。 When the Index method is called for the first time, I want the parameter that holds the resultList of a search to be null.第一次调用 Index 方法时,我希望保存搜索结果列表的参数为 null。 This is because I want to handle an empty resultList differently than a non existing one.这是因为我想以与不存在的结果列表不同的方式处理空结果列表。

So I tried the following:所以我尝试了以下方法:

public async Task<IActionResult> Index(List<SomeModel> resultList = null)

But whenever I start the Web App the list is empty not null.但是每当我启动 Web 应用程序时,列表都是空的,而不是 null。 Why is this?为什么是这样? And do I have a chance to get a list that is null?我是否有机会获得 null 的列表?

I think the best way to get a resultList to null is to create another list.我认为获得resultList的结果列表的最佳方法是创建另一个列表。

 public async Task<IActionResult> Index(List<SomeModel> resultList) 
    {
        if (resultList != null)
        {
          //...
        }
        //there the result will be null,you can do your logic with this result. 
        List<UserAddress> result = null;
        //...
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM