Reputation: 1
im very new to MVC3, and im tasked with adding a search box to my MVC3 controller, and making the controller display results based on a search from the database.
My code pasted below, throws up an error, the error message is pasted also.
public ViewResult Index(string searchString)
{
var newsItem = from n in db.NewsItems
select n;
if (!String.IsNullOrEmpty(searchString))
{
newsItem = newsItem.Where(n => n.Posted.ToUpper().Contains(searchString.ToUpper())
|| n.Posted.ToUpper().Contains(searchString.ToUpper()));
}
return View(db.NewsItems.ToList());
}
Error:
'System.DateTime' does not contain a definition for 'ToUpper' and no extension method 'ToUpper' accepting a first argument of type 'System.DateTime' could be found (are you missing a using directive or an assembly reference?)
C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\Web_Assignment\Web_Assignment\Controllers\NewsController.cs 27 40 Web_Assignment
Upvotes: 0
Views: 324
Reputation: 6772
I think that property Posted has DateTyme type. Then if you really want to search in DateTime column - you need to convert it to string type: newsItem.Where(n => n.Posted.ToString().ToUpper().Contains(searchString.ToUpper())
But maybe you really want to search in Text property?
newsItem.Where(n => n.Text.ToUpper().Contains(searchString.ToUpper())
Upvotes: 2