Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
HI .. am trying search criteria .. when a user type a keyword on text-box it should return relative values from database.

XML
#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())
{
&lt;input type="text" name="txtValue" id="txtValue" placeholder="Need more ?">

&lt;button type="submit" value="Search" style="margin-top: 5px;">Search&lt;/button>
}
</div>
<div style="margin: 35px 0px 0px 90px">

@if (ViewBag.Message == true)
{

&lt;label id="lblMessage" title="Please enter keyword" style="color:red;">Please enter keyword...!&lt;/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
{

&lt;label id="lblErrorMsg" title="Record not fount...!" style="color:red;">Record not found...!&lt;/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();
            }
        }
Posted
Updated 13-Aug-15 17:14pm
v2
Comments
F-ES Sitecore 13-Aug-15 10:05am    
What's your question?
Member 11367931 13-Aug-15 23:07pm    
i need to fetch data in a list from tblBusinessCategory in the view !
Member 11367931 13-Aug-15 23:14pm    
#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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900