Search with dropdown in asp.net MVC
I am new to ASP.NET MVC. I want to use the selected items from my dropdown to search my database table. The dropdown was created from a BOL model that automatically binds to the view.
Below is the 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.
Secondly, I also want the search result to appear on the same page. How should I do it? If I create a separate activity for this, I need to create a separate view for it. Can I use a partial view? If so, how?
Below is the view code
@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>
}
source to share
Try it.
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)
}
}
Here you can change DisplayFor()
to show individual properties of this treatment, such as @Html.DisplayFor(model => model.TreatmentID)
are those
source to share
The above code worked for me, but with a little tweaks. Here are some of the changes I made to your code.
- The parameter in the index action has been changed from
string
tointeger
. - The optional parameter in has
ViewBag.CompanyId
been removed. - Finally, the line
if (!String.IsNullOrWhiteSpace(CompanyID))
and changed toif (CompanyID == 0) { return View(treatmentList);}
The result, however, is great as it worked like a charm! Thank you for your help!
// GET: Admin/ListTreatment
public ActionResult Index(string sortOrder, string sortBy, string Page, int CompanyID = 0)
{
ViewBag.sortOrder = sortOrder;
ViewBag.sortBy = sortBy;
var treatmentList = objBs.GetALL();
//ViewBag.employeeCompany = employeeCompany.Distinct();
switch (sortOrder)
{
case "Asc":
treatmentList = treatmentList.OrderBy(x => x.TreatmentDate).ToList();
break;
case "Desc":
treatmentList = treatmentList.OrderByDescending(x => x.TreatmentDate).ToList();
break;
default:
break;
}
ViewBag.CompanyId = new SelectList(db.Companies, "CompanyId", "CompanyName");
ViewBag.TotalPages = Math.Ceiling(objBs.GetALL().Where(x=>x.CompanyId > 0).Count()/10.0);
int page = int.Parse(Page == null ? "1" : Page);
ViewBag.Page = page;
treatmentList = treatmentList.Skip((page - 1) * 10).Take(10);
if (CompanyID == 0)
{
return View(treatmentList);
}
return View(db.Treatments.Where(x => x.CompanyId == CompanyID).Take(50));
}
source to share