This is a migrated thread and some comments may be shown as answers.

Binding Search Results

2 Answers 81 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Sherman
Top achievements
Rank 2
Sherman asked on 05 Dec 2014, 06:51 PM
Hi there, it's my first time trying to do a search result binding using ASP.NET MVC + ListView. I have included the source codes below. Please let me know if there is anything I should modify, and what I need to do to get it to work.

1. Search Form
01.<div class="content-wrapper">
02.    <div class="search-wrapper">
03.        @using (Html.BeginForm("Search", "Home", FormMethod.Post, new { id="search-form" }))
04.        {
05.            <div class="search-top-panel">
06.                <input id="tbKeywords" name="tbKeywords" type="text"
07.                       class="k-textbox" style="width: 600px; font-size: 24px;"
08.                       required validationmessage="Enter a keyword"
09.                       placeholder="Enter a Keyword" />
10.            </div>
11.            <div class="search-bottom-panel">
12.                <div class="search-bottom-left-panel">
13.                    @Html.ActionLink("Advance Search Options", "Index", "Home", new { search="adv" }, new { @class="standard-link" } )
14.                </div>
15.                <div class="search-bottom-right-panel">
16.                    @(Html.Kendo().Button().Name("btnSearch").Content("Hit It!").HtmlAttributes(new { type = "submit", @class = "k-primary" }))
17.                </div>
18.            </div>
19.        }
20.    </div>
21.</div>

2. Backend Code
01.[HttpPost]
02.public ActionResult Search()
03.{
04.    ViewBag.Keywords = Request.Form [ "tbKeywords" ];
05. 
06.    VideoRepository videoRepository = new VideoRepository ( );
07. 
08.    List<string> keywords = new List<string> ( );
09. 
10.    keywords = Request.Form [ "tbKeywords" ].Split ( new char [ ] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries ).ToList ( );
11. 
12.    IList<Video> lstResults = new List<Video>();
13. 
14.    IList<Video> videos;
15.     
16.    foreach(string keyword in keywords)
17.    {
18.        videos = videoRepository.GetAll ( ).Where ( v => v.VIDEO_TITLE.Contains ( keyword ) || v.VIDEO_DESC.Contains ( keyword ) ).ToList ( );
19. 
20.        foreach(Video v in videos)
21.        {
22.            if ( !lstResults.Contains ( v ) )
23.                lstResults.Add ( v );
24.        }
25.    }
26. 
27.    ViewBag.Results = lstResults;
28. 
29.    return RedirectToAction ( "Index", new { view = "results" } );
30.}

3. ListView
01.@if (Request.QueryString["view"] == "results")
02.{
03.    <div class="ui-memberVideolist" style="width: 100%;">
04.        @(Html.Kendo().ListView<TheWebDevChannel.Models.VideoManagement.Video>(ViewBag.Results)
05.            .Name("lvMemberVideoList")
06.            .TagName("div")
07.            .ClientTemplateId("template")
08.            .Pageable()
09.        )
10.    </div>
11.}

I do recognize that my list view is incomplete code, because I am not exactly sure what else is required.

2 Answers, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 10 Dec 2014, 07:34 AM
Hello Sherman,

The ListView setup seems correct. Providing data in the ListView constructor `ListView<TheWebDevChannel.Models.VideoManagement.Video>(ViewBag.Results)` should be working correct. 

This approach is demonstrated here: ListView Basic Usage.

Can you provide more details like:
 - What's the result when all this executes?
 - Are there JavaScript errors?
 - Are there any server errors?
 - Does the widget render empty?
 - Can you access the ListView client object

You can also check whether all scripts are registered in right order, i.e kendo.all.min.js followed by kendo.aspnetmvc.min.js.

Regards,
Nikolay Rusev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Sherman
Top achievements
Rank 2
answered on 13 Dec 2014, 02:33 PM
Hi there! Thanks for letting me know. All the files are in place. Anyway, I didn't have a chance to replicate the issue, cause I managed to solve it already. Haha!

I just realized that I could do it in 2 ways:

1. ((IEnumerable<TheWebDevChannel.Models.VideoManagement.Video>) ViewBag.Results)

and the other way was much simpler, I just post the results to a new view for the search results (provided my screenshot). Hope this helps others as well.
Tags
ListView
Asked by
Sherman
Top achievements
Rank 2
Answers by
Nikolay Rusev
Telerik team
Sherman
Top achievements
Rank 2
Share this question
or