user1319501
user1319501

Reputation: 93

MVC3 Searchbox Validation

I am new to programming and I have created a searchbox that searches and returns news articles based on thier headline.

What I do not know how to do is return a message to the user in the page when no results are found from the search. I would like a message like "Sorry, No results found" to be displayed.

I am using Visual Studio 2010 ASP.NET with MVC3 Razor and C#.

I have searched around for an answer but had no success, what would be the best way to achieve this, would it be to use JQuery or to modify the controller?

Thanks for your answers in advance

Upvotes: 0

Views: 145

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039438

You could use a view model which will contain an IEnumerable<T> property representing the search results:

public class MyViewModel
{
    public IEnumerable<NewsViewModel> News { get; set; }
}

Now in your view you could test if the News property contains any elements:

@model MyViewModel
...
@if (Model.News != null && Model.News.Any())
{
    ... show the results using the Model.News property
}
else
{
    <div>Sorry, No results found</div>
}

and of course the controller action that is responsible for performing the search will populate this view model and pass it to the view.

Upvotes: 3

Related Questions