In my controller, I filter a list based on parameters that the user first choose. It’s like a search engine.
There is a possibility that the list may return 0 values. While this is not an error, I would like to display some kind of message, like an error message, but all I’ve found so far is using the ModelState or ModelStateDictionary in c# which also requires an exception. But this is no exception, just a condition, so I’m a bit puzzled.
I will write out a bit of code so that you will visually see what I want:
if(listOBJS.count == 0)
{
// DISPLAY THE ERROR!
PopulateDDL1();
PopulateDDL2();
return View(listOBJS);
}
Right, that about what I want to do. How could I proceed? Thanks for the advices.
ModelState doesn’t require an exception.
You can just add a Modelstate error with whatever message you want and use the normal method for checking the ModelState.isValid to decide whether to proceed, or return to the view to show the error.
Alternatively, you could use
ViewBagorViewDatato hole the message, as well.Then in the view they can be displayed