我的目标是为4个不同的下拉菜单制作一个包含多个选项的过滤器。我发布了一个例子,但是只是不同的模型。
我的观点:
{<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Select a Driver <b class="caret"></b></a>
<ul class="dropdown-menu">
<!--Drivers--> @foreach (var item in Model.DriversVM){<!--Added to pull drivers-->
<!--Drivers--> <li>
<input type="checkbox"/>
<label>
<a href="#">
@Html.DisplayFor(modelItem => item.Name)</a>
</label>
</li>}
}
</ul>
</li>
</ul>
</div>
我的控制器:
{
public ActionResult Index()
{
var Drivers = db.Drivers.ToList(); vm.DriversVM = new List();
foreach(驱动程序中的驱动程序驱动程序) { DriverVM NewDriver = new DriverVM(); NewDriver.DriverID = Driver.DriverId; NewDriver.Name = Driver.Name; vm.DriversVM.Add(NewDriver); }
foreach(项目中的项目项目) { NewProject.Driver = db.Drivers.Find(project.Driver.DriverId).Name; }
return View(vm); }
}
任何帮助都会非常感激我是这个编码的新手,我想尽可能多地学习,但现在已经被困了一个星期。
谢谢!
答案 0 :(得分:1)
所以我想出了如何解决这个问题。如果有人遇到类似问题并希望得到答案,我会发布我的结果。
我咨询了我的队友,我是一个假人,因为没有给我的输入一个名字,所以我可以在控制器中用我需要的东西识别它。大多数工作都在控制器中,但需要反映我需要显示的内容。
查看:
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="ind_dropdown_pos">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Select a Driver <b class="caret"></b></a>
<ul class="dropdown-menu" role="menu"> <!--Drivers--> @foreach (var item in Model.DriversVM)
{ <!--pull drivers-->
<li>
<input type="checkbox" name="filterDriver" value="@Html.DisplayFor(modelItem => item.DriverID)" />
<label>
<a href="#">@Html.DisplayFor(modelItem => item.Name)</a>
</label>
</li>} <!--Added to pull drivers-->
</ul>
</li>
</ul>
</div>
控制器:
public ActionResult Index(List<int> filterDriver = null, List<int> filterPerson = null, List<int> filterStage = null, List<int> filterVendor = null)
{
//var projects = db.Projects.Include(p => p.Driver).OrderByDescending(p => p.DateUpdated); //Sorts the project by most recently updated
var Drivers = db.Drivers.ToList();
var Persons = db.People.ToList();
var Stages = db.Stages.ToList();
var Vendors = db.Vendors.ToList();
ProjectIndexVM vm = new ProjectIndexVM();
vm.ProjectsVM = new List<ProjectDetailsVM>();
vm.DriversVM = new List<DriverVM>();
vm.PersonsVM = new List<ProjectPersonVM>();
vm.StagesVM = new List<StageVM>();
vm.VendorsVM = new List<VendorVM>();
vm.FilteredDrivers = new List<string>();
vm.FilteredPersons = new List<string>();
vm.FilteredStages = new List<string>();
vm.FilteredVendors = new List<string>();
vm.ImageLocated = new List<string>();
//var Images = db.ImageTable.ToList(); //add the table name of the table used to store the images
//var projects = db.Projects.Include(p => p.Driver).OrderByDescending(p => p.DateUpdated); //Sorts the most recently updated project ((WORKS))
var projects = from p in db.Projects
select p; // Before trying to sort ((WORKS))
//var projects = from p in db.Projects.Include(p => p.Driver).OrderByDescending(p => p.DateUpdated)
// select p; // ((Did not work))
//var projects = from p in db.Projects
// where db.Projects.Include(p => p.Driver).OrderByDescending(p => p.DateUpdated)
// select p;
if (filterDriver != null)
{
for (int i = 0; i < filterDriver.Count; i++)
{
projects = projects.Where(p => (filterDriver.Contains(p.DriverId) == true));
}
foreach (int item in filterDriver) { vm.FilteredDrivers.Add(db.Drivers.Find(item).Name); }
}
if (filterPerson != null)
{
for (int i = 0; i < filterPerson.Count; i++)
{
projects = projects.Where(x => x.Project_Person.Any(p => filterPerson.Contains(p.PersonId)));
}
foreach (int item in filterPerson) { vm.FilteredPersons.Add(db.People.Find(item).FName + ' ' + db.People.Find(item).LName); }
}
if (filterStage != null)
{
for (int i = 0; i < filterStage.Count; i++)
{
projects = projects.Where(p => (filterStage.Contains(p.Status.OrderByDescending(s => s.Date).FirstOrDefault().StageId) == true));
}
foreach (int item in filterStage) { vm.FilteredStages.Add(db.Stages.Find(item).Name); }
}
if (filterVendor != null)
{
for (int i = 0; i < filterVendor.Count; i++)
{
projects = projects.Where(x => x.ProjectVendors.Any(v => filterVendor.Contains(v.VendorId)));
}
foreach (int item in filterVendor) { vm.FilteredVendors.Add(db.Vendors.Find(item).Name); }
}
foreach (Driver Driver in Drivers)
{
DriverVM NewDriver = new DriverVM();
NewDriver.DriverID = Driver.DriverId;
NewDriver.Name = Driver.Name;
vm.DriversVM.Add(NewDriver);
}
foreach (Person Person in Persons)
{
ProjectPersonVM NewPerson = new ProjectPersonVM();
NewPerson.PersonID = Person.PersonId;
NewPerson.Name = Person.FName + ' ' + Person.LName;
vm.PersonsVM.Add(NewPerson);
}
foreach (Stage Stage in Stages)
{
StageVM NewStage = new StageVM();
NewStage.StageID = Stage.StageId;
NewStage.Name = Stage.Name;
vm.StagesVM.Add(NewStage);
}
foreach (Vendor Vendor in Vendors)
{
VendorVM NewVendor = new VendorVM();
NewVendor.VendorID = Vendor.VendorId;
NewVendor.Name = Vendor.Name;
vm.VendorsVM.Add(NewVendor);
}
foreach (Project project in projects)
{
ProjectDetailsVM NewProject = new ProjectDetailsVM();
NewProject.ProjectID = project.ProjectId;
NewProject.Driver = db.Drivers.Find(project.Driver.DriverId).Name;
NewProject.Title = project.Title;
NewProject.Objective = project.Objective;
NewProject.Vendors = new List<string>();
NewProject.Updates = new List<ProjectUpdateVM>();
NewProject.ProjectFields = new List<ProjectFieldVM>();
//NewProject.ImageTable_item = new List<string>(); //add the item from appropriate table of images
Status projStatus = project.Status.ToList().OrderByDescending(s => s.Date).FirstOrDefault();
NewProject.Status = new ProjectStatusVM();
if (projStatus != null)
{
NewProject.Status.StatusId = projStatus.StatusId;
NewProject.Status.Date = Convert.ToDateTime(projStatus.Date).ToShortDateString();
NewProject.Status.Description = projStatus.Description;
NewProject.Status.Stage = projStatus.Stage.Name;
}
NewProject.Stages = new List<StageVM>();
foreach (Project_Vendor item in project.ProjectVendors)
{
string name = db.Vendors.Find(item.VendorId).Name;
NewProject.Vendors.Add(name);
}
foreach (Project_Field item in project.Project_Field)
{
ProjectFieldVM projField = new ProjectFieldVM();
projField.FieldID = item.FieldId;
projField.Field = item.Field.Name;
projField.Value = item.FieldItem;
NewProject.ProjectFields.Add(projField);
}
NewProject.ProjectPeopleFields = new List<ProjectPersonFieldVM>();
//for each field, store as projectpersonfieldvm, add each person in that group to the list
foreach (Project_Person item in project.Project_Person)
{
ProjectPersonFieldVM personField = new ProjectPersonFieldVM();
personField.FieldID = item.FieldId;
personField.Field = item.Field.Name;
if (NewProject.ProjectPeopleFields.Any(p => p.FieldID == personField.FieldID) != true)
{
NewProject.ProjectPeopleFields.Add(personField);
}
}
foreach (ProjectPersonFieldVM item in NewProject.ProjectPeopleFields)
{
item.FieldPeople = new List<ProjectPersonVM>();
}
foreach (Project_Person item in project.Project_Person)
{
ProjectPersonVM person = new ProjectPersonVM();
person.Name = item.Person.FName + " " + item.Person.LName;
NewProject.ProjectPeopleFields.Find(p => p.FieldID == item.FieldId).FieldPeople.Add(person);
}
foreach (Stage item in db.Stages.ToList())
{
StageVM stage = new StageVM();
stage.StageID = item.StageId;
stage.Name = item.Name;
NewProject.Stages.Add(stage);
}
vm.ProjectsVM.Add(NewProject);
}
return View(vm);
}
修复:
<input type="checkbox" name="filterDriver" value="@Html.DisplayFor(modelItem => item.DriverID)" />