Reputation: 423
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
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
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
Reputation: 62488
you can check in View :
@If(Model.Count == 0)
{
<h3> No Record Found </h3>
}
else
{
//display records
}
Upvotes: 1
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