Ajisha
Ajisha

Reputation: 423

how to check if no record was found?

I am searching data from database according to the character but if data is not available according typed character then how to print message that no record found

Code :

public ActionResult SearchIndex(string searchString)
{
        var movies = from m in db.Movies
                     select m;

        if (!string.IsNullOrEmpty(searchString))
        {
            movies=movies.Where(s=>s.Title.StartsWith(searchString));
        }

        return View(movies);
}

Upvotes: 0

Views: 1936

Answers (4)

Raja Nadar
Raja Nadar

Reputation: 9499

In your Razor, do something like:

if (!Model.Any())
{
 // No record found. Display appropriate message.
}
else
{
 // search results were found, render them.
}

Upvotes: 2

user3383479
user3383479

Reputation:

It depends, you have many possibilities: You can also use a ViewData object.

   public ActionResult SearchIndex(string searchString)
  {
    var movies = from m in db.Movies
                 select m;

    if (!string.IsNullOrEmpty(searchString))
    {
        movies=movies.Where(s=>s.Title.StartsWith(searchString));
    }
    if(movies == null)
    {
     ViewData["NoMovies"] = "No Movies found";
     movies = from m in db.Movies
                 select m; // As your movie will be null or You can pass a new movies()
    }

    return View(movies); //
    }

in your view:

@{ if (ViewData["NoMovies"] != null) {
 @ViewData["NoMovies"] 
<br />
  }

Upvotes: 0

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

you can check in View :

@If(Model.Count == 0)
{
<h3> No Record Found </h3>
}
else
{
//display records
}

Upvotes: 1

Surya Deepak
Surya Deepak

Reputation: 431

If Your method is called on keyup from an ajax call then you can return

 if (!string.IsNullOrEmpty(searchString)){
      movies=movies.Where(s=>s.Title.StartsWith(searchString));
  }
  if(movies==null){
      return JSON("No Records found")
   } 

You can access the returned json object within your success callback and you can print the data directly

Upvotes: 0

Related Questions