我是ASP.NET MVC的新手。我想使用我的下拉列表中的选定项目来搜索我的数据库表。下拉列表是从BOL模型生成的,该模型自动绑定到视图。
以下是我的代码段
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();
}
以下是视图的界面。
其次,我还希望搜索结果出现在同一页面上。我该怎么做呢?如果我为此创建一个单独的操作,我将需要为它创建一个单独的视图。可以使用局部视图吗?如果是这样的话?
以下是视图的代码
@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>
}
答案 0 :(得分:3)
试试这个。
控制器操作:
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));
}
查看代码:
@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)
}
}
您可以在此处更改DisplayFor()
以显示给定治疗的各个属性,例如@Html.DisplayFor(model => model.TreatmentID)
等
答案 1 :(得分:1)
以上代码对我有用,但调整很少。以下是我对您的代码进行的一些修改。
string
更改为integer
。ViewBag.CompanyId
中的可选参数已被删除。if (!String.IsNullOrWhiteSpace(CompanyID))
并更改为if (CompanyID == 0) { return View(treatmentList);}
然而结果很棒,因为它像魅力一样!谢谢你的帮助!
// 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));
}
答案 2 :(得分:0)
首先:对于实体框架id应该是可空的,因此它可以被接受为参数,action参数应该是int? CompanyID
第二:(CompanyID == 0)
应为(CompanyID == null)