Home MVC: Search by name of the video
Reply: 1

MVC: Search by name of the video

mariie Published in 2017-12-07 18:36:53Z

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" })
                        <button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>

My method:

public ActionResult SearchVideo(string searchVideo)
    var model = new VideoListViewModel();
    model.VideoList = repo.SearchVideo(searchVideo).ToList();

    return View("Search", model);

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;" />
                Premiera: @video.Date

My method in repository:

public IEnumerable<Video> SearchVideo(string search)
        ECinemaDBEntities db = new ECinemaDBEntities();

        return db.Video.Where(x => x.VideoName == search);
    catch (Exception)
Shyju Reply to 2017-12-07 19:03:52Z

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));
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.330977 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO