0

Hi I have a question how can I improve my code that I can search through my database by putting at least one letter in textbox not all the title. Now it's searching but I need to put whole title to search something.

My search bar:

@using (Html.BeginForm("SearchVideo", "Video", FormMethod.Post, new { @class = "navbar-form navbar-left", @role = "search" }))
                    {
                        <div class="form-group">
                            @Html.TextBox("searchVideo", (object)null, new { @class = "form-control", @placeholder = "Wyszukaj film" })
                        </div>
                        <button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
                    }

My VideoListViewModel:

public class VideoListViewModel
{
    public IEnumerable<Video> VideoList { get; set; }
}

My searching View:

    <div class="list-group">
        @foreach (var video in Model.VideoList)
        {
            <a href="/Video/GetVideo?id=@video.Id" class="list-group-item clearfix">
                <img src="~/Images/@video.ImageUrl" style="width: 80px; height: 100px; padding: 5px;" />
                Premiere: @video.Date
                <br/>
            </a>
            <br/>
        }
    </div>
mariie
  • 45
  • 1
  • 10

1 Answers1

1

You can use the Contains method inside your where clause

return db.Video.Where(x => x.VideoName.Contains(search));

Contains method returns true when a string exist anywhere in the other string. If you are looking to get the records where the VideoName starts with the search parameter value ,you may use StartsWith method.

return db.Video.Where(x => x.VideoName.StartsWith(search));
Shyju
  • 214,206
  • 104
  • 411
  • 497