Website
필드가 VarChar 인 mySQL 데이터베이스가 있습니다. 이러한 필드 중 일부는 데이터베이스에서 null이됩니다.Linq을 사용하여 null이 아닌 경우
데이터베이스 정보를 보내는 MVC 응용 프로그램이 있습니다.
색인 페이지에서 특정 열을 기준으로 필터링 할 수 있도록 필터를 설정하려고합니다. 이 작업을 수행하려면 Request.QueryString
을 사용하고 있습니다.
switch (Request.QueryString["FilterOptionSelect"])
{ case "CountyName":
if (!string.IsNullOrEmpty(filteredText))
{
filteredText = filteredText.ToUpper();
var modelFiltered = from n in model
where n.CountyName.ToUpper().Contains(filteredText)
select n;
return View(modelFiltered);
}
break;
case "Website":
if (!string.IsNullOrEmpty(filteredText))
{
filteredText = filteredText.ToUpper();
var modelFiltered = from n in model
where n.CountyWebsite.ToUpper().Contains(filteredText)
select n;
return View(modelFiltered);
}
break;
}
유일한 문제는 웹 사이트의 경우입니다. 이 웹 사이트 사례의 배경에 Object reference not set to an instance of an object.
이 나와 있습니다. 디버깅 할 때 내 모델은 null이 아닙니다 (130 개 이상의 항목이 있습니다 ... 일부는 웹 사이트 정보 및 일부는 웹 사이트 정보 없음).
나는 이미 동일한 문제가있는 Lambda 방법을 사용해 보았습니다. 나는 또한 where n.CountyWebsite.ToUpper().Contains(filteredText) && n.CountyWebsite != null
을 사용해 보았는데 어느 것도 작동하지 않았다.
요청 개체를 직접 사용하지 않는 것이 좋습니다. MVC는 이러한 세부 사항을 숨 깁니다. FilterOptionsSelect라는 속성 또는 메서드 매개 변수를 사용하여 모델을 만들면 MVC가 값을 밀어 넣습니다. MVC로 빌드 할 때 요청 또는 응답을 직접 사용하는 이유는 거의 없습니다. –