Reputation: 39
I am trying to make search with MVC 5, I got the logic but what I need is to have method that returns 3 different list objects
I have view model in MVC 5
public class SearchViewModel
{
public List<Article> ArticleSearch { get; set; }
public List<Albums> AlbumsSearch { get; set; }
public List<Blog> BlogSearch { get; set; }
public List<PDFModel> PDFSearch { get; set; }
}
In my controller:
public ActionResult Index()
{
using (DBContext db = new DBContext())
{
string xyz = "f";
var Search = new SearchViewModel()
{
AlbumsSearch = helper.Search(db.Albums, xyz) <!-- Here i get error -->
};
return View(Search);
}
}
the helper.Search :
public static IList Search(object obj,string SearchString)
{
using (DBContext db = new DBContext())
{
if (obj is Albums)
{
var AlbumSearch = db.Albums.Where(x => x.AlbumName.Contains(SearchString) ||
x.AlbbumSmallDesc.Contains(SearchString) || x.AlbumNameEnglish.Contains(SearchString)||
x.AlbbumSmallDescEnglish.Contains(SearchString)).ToList();
return AlbumSearch as List<Albums>;
}
else if(obj is Article)
{
var ArticleSearch = db.Article.Where(x => x.ArticleSubject.Contains(SearchString)||x.ArticleSubjectEnglish.Contains(SearchString)||
x.ArticleMessage.Contains(SearchString)||x.ArticleMessageEnglish.Contains(SearchString)).ToList();
return ArticleSearch;
}
else
{
return null;
}
}
}
I want to make a method that returns a list to make the search
I get this error:
Cannot implicitly convert type 'System.Collections.IList' to 'System.Collections.Generic.List<MohamedJibril.Models.Albums>'. An explicit conversion exists (are you missing a cast?)
Upvotes: 0
Views: 5696
Reputation: 27009
The message is pretty clear, you are trying to convert IList
to IList<T>
. Your method returns the former and your view model has the latter.
Your search method gives you no advantage. You have all those if statements. What are you going to do if you need 30 different types, write 30 different if statements? Instead do this:
public static IList<Album> SearchAlbums(string SearchString)
{
using (DBContext db = new DBContext())
{
var AlbumSearch = db.Albums.Where(x => x.AlbumName.Contains(SearchString) ||
x.AlbbumSmallDesc.Contains(SearchString) || x.AlbumNameEnglish.Contains(SearchString) ||
x.AlbbumSmallDescEnglish.Contains(SearchString)).ToList();
return AlbumSearch;
}
}
Create the same methods for the other types too.
Upvotes: 0
Reputation: 3703
You should probably use a different search function for different classes. Having said that, you can use the same search function with a bit of a hack. Define an extension method to convert your List<T>
into a List<object>
. Then just return a List<object>
from your method.
public static class ListExtensions
{
public static List<Object> ToObjectList<T>(this List<T> list) where T : class
{
var objectList = new List<object>();
list.ForEach(t => objectList.Add(t));
return objectList;
}
}
List<object> Search(int i)
{
if (i == 0)
{
return new List<Album>().ToObjectList();
}
else
{
return new List<Article>().ToObjectList();
}
}
Update
You can then convert back when you're calling the search using an extension method like this.
public static List<T> FromObjectList<T>(this List<object> list) where T : class
{
var result = new List<T>();
list.ForEach(t => result.Add(t as T));
return result;
}
List<Album> albums = Search(0).FromObjectList<Album>();
Upvotes: 2