HI .. am trying search criteria .. when a user type a keyword on text-box it should return relative values from database.
#1) U can see a text box in my view
<input type="text" name="txtValue" id="txtValue" placeholder="Need more ?">
#2) when i enter any keyword in this text-box. i should fetch data from table "tblBusinessCategory" using 'Contains' command.
#3) fetched data should be displayed in a list
<div class="searchform cf">
@using (Html.BeginForm())
{
<input type="text" name="txtValue" id="txtValue" placeholder="Need more ?">
<button type="submit" value="Search" style="margin-top: 5px;">Search</button>
}
</div>
<div style="margin: 35px 0px 0px 90px">
@if (ViewBag.Message == true)
{
<label id="lblMessage" title="Please enter keyword" style="color:red;">Please enter keyword...!</label>
}
else
{
if (Model != null)
{
if (Model.Count() != 0)
{
<div>
<h2>Searched for ""</h2>
</div>
<div>
@foreach (var item in Model)
{
<h2><a href="" style="color: #063AD8;">@item.tblBusinessCategory.BusinessName</a></h2>
<h3>@item.tblBusinessCategory.BusinessCategory</h3>
<h4 style="color: rgb(0, 145, 0);">@item.tblBusinessCategory.BusinessDescription</h4>
}
</div>
}
else
{
<label id="lblErrorMsg" title="Record not fount...!" style="color:red;">Record not found...!</label>
}
}
}
</div>
Controller:
public ActionResult Index(string txtValue)
{
xxxEntities db = new xxxEntities();
if (txtValue.Length > 0)
{
string[] keywords = txtValue.Trim().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
this.keywords = keywords.ToList();
var results = (from u
in db.tblBusinessCategories
where u.BusinessName.Contains(txtValue)
|| u.BusinessCategory.Contains(txtValue) || u.BusinessDescription.Contains(txtValue)
select u).ToList();
return View(results);
}
else
{
ViewBag.Message = true;
return View();
}
}