Reputation:
When I try to compile my code then ArgumentNullException error occurs on 'Where' clause. I think I'm doing everything well on this. Do I need any start value (in ActionResult parameter)?
Controller:
public class HomeController : Controller
{
private IDocuments _docs;
public ActionResult Index(string custNr)
{
var docsModel = _docs.GetAll();
var model = docsModel
.Where(w => w.docsModel.ACCOUNT.Contains(custNr) || custNr == null) //here shows me an error
.Select(s => new DocumentsModel
{
Company = s.docsModel.COMPANY,
CustName = s.docsModel.NAME,
Account = s.docsModel.ACCOUNT.Trim(),
})
.OrderBy(o => o.DATE);
return View(model);
}
View:
<th>
@using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
<p>
Name: @Html.TextBox("custNr")
<input type="submit" value="Search" />
</p>
}
</th>
Need to add that in Account column is populated with values, has no nulls
Upvotes: 0
Views: 25
Reputation: 4629
It should help to invert your OR-Expression:
.Where(w => custNr == null || w.docsModel.ACCOUNT.Contains(custNr) )
An OR-Clause always gets evaluated from left to right. The way you did it, it will first execute the Contains
function and afterwards will check if the input parameter is null.
The other way around the Contains
-function will not be called if the argument is null as the first OR-clause is fulfilled and it will not execute the second one.
Contains
on a string should never be called with null as input parameter.
Exceptions
ArgumentNullException
value is null.
Upvotes: 1