max b
max b

Reputation: 107

Search method by string value MVC 5

A a part of my project i need to find a way to search my object by a string and show a result in view. Your help is appreciated. in my MainMedia view i have a sidesection were i manually pass a string value to a SearchMedia method:

@section SideBar{
<ul>        
    <li> @Html.ActionLink("Astronomy", "SearchMedia", new {searchString = "Astronomy" })</li>
    <li> @Html.ActionLink("World", "SearchMedia", new { searchString = "World" })</li>
    <li> @Html.ActionLink("Movies", "SearchMedia", new { searchString = "Movies" })</li>
</ul>
}

This method should check every object if TagsEnum string and then display an object in SearchMedia view.

Here is my Media class

public class Media
{
    public int Id { get; set; }
    public string title { get; set; }
    public string description { get; set; }
    public string body { get; set; }
    public string ImagePath { get; set; }
    public string VideoLink { get; set; }
    public string Source { get; set; }
    public string tags { get; set; }
    public TagsEnum TagsEnum { get; set; }
}

TagsEnum Class

 public enum TagsEnum
    {
        [Display(Name = "Astronomy and space")]
        Astronomy,
        [Display(Name = "World around us")]
        World,
        [Display(Name = "Movies, video")]
        Movies
}

and finaly MediaMainController SearchMedia method

public ActionResult SearchMedia(string searchString)
{
    db.Medias.Where(i => i.TagsEnum.ToString() == searchString);
    return View(db.Medias.OrderBy(it => it.Title));
}

As i understand .Where() should find a match and return an object, however it is not working. How i can sort it out? Perhaps there are other ways to do it? Thank you

Update I have changed it like this:

var result = db.Medias.Where(TagsEnum => TagsEnum.ToString() == searchString);
 return View(result.OrderBy(it => it.title)); 

but i still dont see the results to be sorted by search

Update 2 I have a class MediaViewModel which i use to create a list of objects, it looks like this:

 public class MediaViewModel
    {
        public List<Media> media { get; set; }
        public List<Video> video { get; set; }

    }

If i set up SearchMedia View like this

@model PhClub.Models.MediaViewModel
@foreach (var b in Model.media)
{}

i'm getting an error: The model item passed into the dictionary is of type System.Linq.Enumerable+WhereListIterator 1[PhClub.Models.Media], but this dictionary requires a model item of type PhClub.Models.MediaViewModel. If i set it up as

`@model IEnumerable<PhClub.Models.Media>
    @foreach (var b in Model)
    {}`

it is saying Values of type 'Media' can not be converted to string. I think i need to change SearchMedia method to support MediaView class, but i didnt figure it out yet. Help is appreciated

Upvotes: 0

Views: 1231

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222582

You should assign it to a variable and use it,

 var result = db.Medias.Where(i => i.TagsEnum.ToString() == searchString);
 return View(result.OrderBy(it => it.Title));

Upvotes: 2

Related Questions