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
2. Backend Code
3. ListView
I do recognize that my list view is incomplete code, because I am not exactly sure what else is required.
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.