Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching with a dropdown list in asp.net MVC

I'm new to ASP.NET MVC. I want to use selected items from my dropdownlist to search my database table. The dropdownlist was generated from a BOL model which automatically binds to the view.

Below are my code snippet

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using BOL;

namespace DentiCareApp.Areas.Admin.Controllers
{
    [AllowAnonymous]
    public class GenerateInvoiceController : Controller
    {
        private TreatmentBs objBs;

        public GenerateInvoiceController()
        {
                objBs = new TreatmentBs();
        }
        // GET: Admin/GenerateInvoice
        public ActionResult Index(string CompanyID)
        {
            DentiCareEntities db = new DentiCareEntities();
            ViewBag.CompanyId = new SelectList(db.Companies, "CompanyId", "CompanyName");

            if (CompanyID == null)
            {
                return View();
            }
            else
            {
                return View(db.Treatments.Where(x => x.Company == CompanyID.Take(50)));
            }
            //return View();
        }

Also below is the interface of view.

enter image description here

Secondly, I also want the search result to appear on the same page. How do I do this? If I create a separate action for this, I will need to create a separate view for it. Can partial view be used? If so how?

Below is the code to the View

    @model BOL.GenerateInvoice
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    <p></p>
    <p></p>
    <p></p>
    <h2>Quickly Generate Invoice</h2>
    @using (Html.BeginForm("Index", "GenerateInvoice", FormMethod.Get))
    {
        @Html.AntiForgeryToken()
        <div class="">
            <div>
                @Html.DropDownList("MyCompany.CompanyId", (IEnumerable<SelectListItem>)ViewBag.CompanyId, "Select Company", new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.MyCompany.CompanyId, "", new { @class = "text-danger" })
                <input type="submit" value="Search" class="btn btn-primary" />
            </div>
        </div>
    }
like image 947
Guzzyman Avatar asked Jan 21 '26 16:01

Guzzyman


1 Answers

Try this.

Controller action:

public ActionResult Index(string CompanyID)
{
    DentiCareEntities db = new DentiCareEntities();
    ViewBag.CompanyId = new SelectList(db.Companies, "CompanyId", "CompanyName", CompanyID);    // preselect item in selectlist by CompanyID param

    if (!String.IsNullOrWhiteSpace(CompanyID))
    {
        return View();
    }

    return View(db.Treatments.Where(x => x.CompanyID == CompanyID).Take(50));
}

View code:

@model IEnumerable<Treatment>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Quickly Generate Invoice</h2>

@using (Html.BeginForm("Index", "GenerateInvoice", FormMethod.Get))
{
    @Html.AntiForgeryToken()

    @Html.DropDownList("CompanyId", (SelectList)ViewBag.CompanyId, "Select Company", new { @class = "form-control" })
    <input type="submit" value="Search" class="btn btn-primary" />
}

@if(Model != null && Model.Any())
{
    foreach(var item in Model)
    {
        @Html.DisplayFor(model => item)
    }
}

You can change the DisplayFor() here to show individual properties of the given Treatment, such as @Html.DisplayFor(model => model.TreatmentID) and such

like image 147
Brad C Avatar answered Jan 23 '26 04:01

Brad C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!