[英]Searching with a dropdown list in asp.net MVC
I'm new to ASP.NET MVC. 我是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.
下拉列表是从BOL模型生成的,该模型自动绑定到视图。
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. 以下是视图的界面。
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 下面是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>
}
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 您可以在此处更改
DisplayFor()
以显示给定Treatment的各个属性,例如@Html.DisplayFor(model => model.TreatmentID)
等
The Above code worked for me but with little tweaks. 上面的代码对我有用,但很少调整。 Here are few modification I made to your code.
以下是我对您的代码进行的一些修改。
string
to integer
. string
更改为integer
。 ViewBag.CompanyId
was removed. ViewBag.CompanyId
的可选参数ViewBag.CompanyId
被删除。 if (!String.IsNullOrWhiteSpace(CompanyID))
and changed to if (CompanyID == 0) { return View(treatmentList);}
if (!String.IsNullOrWhiteSpace(CompanyID))
并更改为if (CompanyID == 0) { return View(treatmentList);}
The result however is great as it worked like a charm! 结果却很棒,因为它像魅力一样! Thanks 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));
}
First : for entity framework id should be nullable, so it can be accepted as argument, the action parameter should be int? CompanyID
首先:对于实体框架id应该是可空的,因此它可以被接受为参数,action参数应该是
int? CompanyID
int? CompanyID
Second : the comparison is not correct with (CompanyID == 0)
第二:比较不正确
(CompanyID == 0)
It should be (CompanyID == null)
它应该是
(CompanyID == null)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.