Search code examples
c#asp.net-mvcsearchbar

Add a search bar in asp net mvc


I need to add a search bar on my index page, and the result of the research will be show on another page.

What I've tried:

The view

<form class="row contact-form rounded-pill link no-gutters" id="contact-form-data">
    @using (Html.BeginForm("RechercheResto", "Client", FormMethod.Post))
    {
        @Html.AntiForgeryToken()
        <div class="col-12 col-lg-8 d-inline-block d-lg-flex align-items-center">
            <div class="form-group">
                <label><i class="lni lni-restaurant" aria-hidden="true"></i></label>
                <input type="text" name="nomResto" placeholder="Nom du restaurant" class="form-control">
            </div>
        </div>
        <div class="col-12 col-lg-4">
            <i class="fa fa-spinner fa-spin mr-2 d-none" aria-hidden="true"></i>
            <input type="submit" class="btn main-btn rounded-pill w-100 h-100 contact_btn" value="Rechercher le restaurant" />
        </div>
    }
</form>

The controller

[ValidateAntiForgeryToken]
public ActionResult RechercheResto(string nomResto)
{
    var restoList = from r in db.Restaurants select r;
    if (!string.IsNullOrEmpty(nomResto))
    {
        restoList = restoList.Where(r => r.name.Contains(nomResto));
    }
    return View(restoList.ToList());
}

Solution

  • At the time of returning View(restoList.ToList()) you can add viewname in first parameter and second will be your result.ToList().