如何将下拉列表中的选项限制为仅有效选项?
让我进一步解释一下,我的数据库中有很多人。他们有一对多的选项来制定行动计划,我的页面显示了所有制定了行动计划的人。我还有下拉列表来过滤所有有行动计划的人......这很好用。下拉列表工作正常,我无法得到的部分是有一个包含人员姓名的下拉列表过滤器(以查找该人的所有行动计划),但下拉列表显示数据库中的所有名称,而不仅仅是这些名称有行动计划的人,有什么办法可以限制下拉列表只显示有行动计划的人吗?
抱歉,如果已经有关于此的帖子...我一直在寻找,但找不到解决方案,
谢谢
。这可能会有所帮助......是的,我正在使用 MVC 在我看来
<td><%=Html.DropDownList("FORMER_BORROWER", "All")%></td>
<td><%=Html.DropDownList("CURRENT_OWNERSHIP_ENTITY", "All")%></td>
<td><%=Html.DropDownList("RESPONSIBLE_PARTY", "All")%></td>
<td><%=Html.DropDownList("COMPLETED", "All")%></td>
<td><%=Html.DropDownList("TARGET_DATE", "All")%></td>
这是在我的控制器中
var predicate = PredicateBuilder.False<ACTION_PLAN>();
predicate = predicate.Or(p => p.COMPLETED != null);
// former borrower
if ((Request.Form["FORMER_BORROWER"] != null) && Request.Form["FORMER_BORROWER"] != "")
{
FormerBorrower = Request.Form["FORMER_BORROWER"];
ViewData["FORMER_BORROWER"] = new SelectList((from n in _db.Loans where (n.FORMER_BORROWER_NAME != null) select new { n.FORMER_BORROWER_NAME, FORMER_BORROWER_NAMEID = n.FORMER_BORROWER_NAME }).Distinct().ToList(), "FORMER_BORROWER_NAMEID", "FORMER_BORROWER_NAME", FormerBorrower);
predicate = predicate.And(p => p.Loan.FORMER_BORROWER_NAME == FormerBorrower);
}
else
{
FormerBorrower = "";
ViewData["FORMER_BORROWER"] = new SelectList((from n in _db.Loans where (n.FORMER_BORROWER_NAME != null) select new { n.FORMER_BORROWER_NAME, FORMER_BORROWER_NAMEID = n.FORMER_BORROWER_NAME }).Distinct().ToList(), "FORMER_BORROWER_NAMEID", "FORMER_BORROWER_NAME");
}
// current ownership entity
if ((Request.Form["CURRENT_OWNERSHIP_ENTITY"] != null) && Request.Form["CURRENT_OWNERSHIP_ENTITY"] != "")
{
CurrentOwnershipEntity = Request.Form["CURRENT_OWNERSHIP_ENTITY"];
ViewData["CURRENT_OWNERSHIP_ENTITY"] = new SelectList((from n in _db.DOM_CURRENT_OWNERSHIP_ENTITies select n).ToList(), "CURRENT_OWNERSHIP_ENTITY", "CURRENT_OWNERSHIP_ENTITY", CurrentOwnershipEntity);
predicate = predicate.And(p => p.Loan.DOM_CURRENT_OWNERSHIP_ENTITY.CURRENT_OWNERSHIP_ENTITY == CurrentOwnershipEntity);
}
else
{
CurrentOwnershipEntity = null;
ViewData["CURRENT_OWNERSHIP_ENTITY"] = new SelectList((from n in _db.DOM_CURRENT_OWNERSHIP_ENTITies select n).ToList(), "CURRENT_OWNERSHIP_ENTITY", "CURRENT_OWNERSHIP_ENTITY");
}
// responsible party
if ((Request.Form["RESPONSIBLE_PARTY"] != null) && Request.Form["RESPONSIBLE_PARTY"] != "")
{
ResponsibleParty = Request.Form["RESPONSIBLE_PARTY"];
ViewData["RESPONSIBLE_PARTY"] = new SelectList((from n in _db.ACTION_PLANs select n).ToList(), "RESPONSIBLE_PARTY", "RESPONSIBLE_PARTY", ResponsibleParty);
predicate = predicate.And(p => p.RESPONSIBLE_PARTY == ResponsibleParty);
}
else
{
ResponsibleParty = null;
ViewData["RESPONSIBLE_PARTY"] = new SelectList((from n in _db.ACTION_PLANs select n).ToList(), "RESPONSIBLE_PARTY", "RESPONSIBLE_PARTY");
}
// completed
if ((Request.Form["COMPLETED"] != null) && Request.Form["COMPLETED"] != "")
{
Completed = System.Convert.ToBoolean(Request.Form["COMPLETED"]);
ViewData["COMPLETED"] = new SelectList((from n in _db.ACTION_PLANs where (n.COMPLETED != null) select new { n.COMPLETED}).Distinct().ToList(), "COMPLETED", "COMPLETED", Completed);
predicate = predicate.And(p => p.COMPLETED == Completed);
}
else
{
Completed = System.Convert.ToBoolean(null);
ViewData["COMPLETED"] = new SelectList((from n in _db.ACTION_PLANs where (n.COMPLETED != null) select new { n.COMPLETED }).Distinct().ToList(), "COMPLETED", "COMPLETED");
}
// target date
if ((Request.Form["TARGET_DATE"] != null) && Request.Form["TARGET_DATE"] != "")
{
TargetDate = System.Convert.ToDateTime(Request.Form["TARGET_DATE"]).ToString("MM/dd/yyyy");
ViewData["TARGET_DATE"] = new SelectList((from n in _db.ACTION_PLANs select n).ToList(), "TARGET_DATE", "TARGET_DATE", TargetDate);
predicate = predicate.And(p => p.TARGET_DATE == Convert.ToDateTime(TargetDate));
}
else
{
TargetDate = "";
ViewData["TARGET_DATE"] = new SelectList((from n in _db.ACTION_PLANs select n).ToList(), "TARGET_DATE", "TARGET_DATE");
}
allow me to further explain, i have a lot of people in my db. and they have a one to many option to have an action plan, my page displays all of the people that have action plans. i also have dropdownlist to filter threw all of the people that have action plans... this works fine. the dropdownlists work fine, the part i cannot get is that there is a dropdownlist filter with the persons name(to find all of the action plans for that one person), but the dropdownlist shows ALL the names in the db and not just the ones that have action plans, is there any way to limit the dropdownlist to only show people that HAVE action plans?
sorry if there is already a post on this... i have been looking and looking but cannot find the solution
thanks
p.s. this might help.... and yes i am using MVC
this is in my view
<td><%=Html.DropDownList("FORMER_BORROWER", "All")%></td>
<td><%=Html.DropDownList("CURRENT_OWNERSHIP_ENTITY", "All")%></td>
<td><%=Html.DropDownList("RESPONSIBLE_PARTY", "All")%></td>
<td><%=Html.DropDownList("COMPLETED", "All")%></td>
<td><%=Html.DropDownList("TARGET_DATE", "All")%></td>
this is in my controller
var predicate = PredicateBuilder.False<ACTION_PLAN>();
predicate = predicate.Or(p => p.COMPLETED != null);
// former borrower
if ((Request.Form["FORMER_BORROWER"] != null) && Request.Form["FORMER_BORROWER"] != "")
{
FormerBorrower = Request.Form["FORMER_BORROWER"];
ViewData["FORMER_BORROWER"] = new SelectList((from n in _db.Loans where (n.FORMER_BORROWER_NAME != null) select new { n.FORMER_BORROWER_NAME, FORMER_BORROWER_NAMEID = n.FORMER_BORROWER_NAME }).Distinct().ToList(), "FORMER_BORROWER_NAMEID", "FORMER_BORROWER_NAME", FormerBorrower);
predicate = predicate.And(p => p.Loan.FORMER_BORROWER_NAME == FormerBorrower);
}
else
{
FormerBorrower = "";
ViewData["FORMER_BORROWER"] = new SelectList((from n in _db.Loans where (n.FORMER_BORROWER_NAME != null) select new { n.FORMER_BORROWER_NAME, FORMER_BORROWER_NAMEID = n.FORMER_BORROWER_NAME }).Distinct().ToList(), "FORMER_BORROWER_NAMEID", "FORMER_BORROWER_NAME");
}
// current ownership entity
if ((Request.Form["CURRENT_OWNERSHIP_ENTITY"] != null) && Request.Form["CURRENT_OWNERSHIP_ENTITY"] != "")
{
CurrentOwnershipEntity = Request.Form["CURRENT_OWNERSHIP_ENTITY"];
ViewData["CURRENT_OWNERSHIP_ENTITY"] = new SelectList((from n in _db.DOM_CURRENT_OWNERSHIP_ENTITies select n).ToList(), "CURRENT_OWNERSHIP_ENTITY", "CURRENT_OWNERSHIP_ENTITY", CurrentOwnershipEntity);
predicate = predicate.And(p => p.Loan.DOM_CURRENT_OWNERSHIP_ENTITY.CURRENT_OWNERSHIP_ENTITY == CurrentOwnershipEntity);
}
else
{
CurrentOwnershipEntity = null;
ViewData["CURRENT_OWNERSHIP_ENTITY"] = new SelectList((from n in _db.DOM_CURRENT_OWNERSHIP_ENTITies select n).ToList(), "CURRENT_OWNERSHIP_ENTITY", "CURRENT_OWNERSHIP_ENTITY");
}
// responsible party
if ((Request.Form["RESPONSIBLE_PARTY"] != null) && Request.Form["RESPONSIBLE_PARTY"] != "")
{
ResponsibleParty = Request.Form["RESPONSIBLE_PARTY"];
ViewData["RESPONSIBLE_PARTY"] = new SelectList((from n in _db.ACTION_PLANs select n).ToList(), "RESPONSIBLE_PARTY", "RESPONSIBLE_PARTY", ResponsibleParty);
predicate = predicate.And(p => p.RESPONSIBLE_PARTY == ResponsibleParty);
}
else
{
ResponsibleParty = null;
ViewData["RESPONSIBLE_PARTY"] = new SelectList((from n in _db.ACTION_PLANs select n).ToList(), "RESPONSIBLE_PARTY", "RESPONSIBLE_PARTY");
}
// completed
if ((Request.Form["COMPLETED"] != null) && Request.Form["COMPLETED"] != "")
{
Completed = System.Convert.ToBoolean(Request.Form["COMPLETED"]);
ViewData["COMPLETED"] = new SelectList((from n in _db.ACTION_PLANs where (n.COMPLETED != null) select new { n.COMPLETED}).Distinct().ToList(), "COMPLETED", "COMPLETED", Completed);
predicate = predicate.And(p => p.COMPLETED == Completed);
}
else
{
Completed = System.Convert.ToBoolean(null);
ViewData["COMPLETED"] = new SelectList((from n in _db.ACTION_PLANs where (n.COMPLETED != null) select new { n.COMPLETED }).Distinct().ToList(), "COMPLETED", "COMPLETED");
}
// target date
if ((Request.Form["TARGET_DATE"] != null) && Request.Form["TARGET_DATE"] != "")
{
TargetDate = System.Convert.ToDateTime(Request.Form["TARGET_DATE"]).ToString("MM/dd/yyyy");
ViewData["TARGET_DATE"] = new SelectList((from n in _db.ACTION_PLANs select n).ToList(), "TARGET_DATE", "TARGET_DATE", TargetDate);
predicate = predicate.And(p => p.TARGET_DATE == Convert.ToDateTime(TargetDate));
}
else
{
TargetDate = "";
ViewData["TARGET_DATE"] = new SelectList((from n in _db.ACTION_PLANs select n).ToList(), "TARGET_DATE", "TARGET_DATE");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这更多的是一个 Sql 问题,基本上您希望下拉列表显示此请求的结果:
伪代码:
内部联接使您的查询仅返回在 ActionPlan 表中具有记录的表 person 中的行。
This is more of a Sql question basically you want your dropdown to show the result of this request :
Pseudo code :
The inner join make your query return only rows from the table person that have a record in the ActionPlan table.
在 Html.DropDownList 上进行一些快速谷歌搜索似乎表明这是 ASP.Net MVC - 这是正确的吗?
如果您使用 MVC,我很惊讶您竟然在屏幕上显示数据库数据,而不知道在哪里可以过滤该数据。到目前为止,您是否自动生成了所有内容?
我没有太多使用 MVC,只是在 Visual Studio 中整理了一个快速而肮脏的示例。您应该看到两个文件夹,一个称为“Views”,一个称为“Controllers”。
Views 文件夹中应该有一个视图文件,它是您提供的标记所在的位置。
在 Controllers 文件夹中应该有一个用于视图的控制器文件,该文件将是一个内容如下所示的类:
绑定 DropDownList 控件的代码如下:
ViewData["FORMER_BORROWER"] = ...
现在,在 SelectList 中,第一个参数只是一个 IEnumerable(在我的例子中访问数据库),因此您可以使用 LINQ 来过滤该对象。
下面是一个简单的示例,其中我提供的 IEnumerable 是在代码中创建的,但访问数据库的代码可以以类似的方式使用。不是我如何使用联接来仅提供所需的值。
上面的代码现在将仅显示 DDL 中的一项 - 由于连接和位置而产生的 Cycling 项。
使用你的控制器代码,你最终会得到类似下面的代码(仅关注 FORMER_BORROWER if/else 分支的一半)
对上面答案的一个评论是,虽然这可行,但我个人会考虑调整设计,创建某种服务供控制器调用,抽象出一些数据访问逻辑。理想情况下,您现在应该将 ObjectContext 放在控制器层中,但放在服务层中,从而提供更集中的控制器,在访问数据层的同时不会尝试控制视图。
Doing some quick googling on Html.DropDownList seems to point to this being ASP.Net MVC - is that correct?
If you are using MVC I'm surprised that you have made is as far as displaying database data on screen without having an idea where you can filter that data. Did you autogenerate everything so far perhaps?
I haven't used MVC very much but just put together a quick and dirty example in Visual Studio. You should see a two folders, one called Views and one called Controllers.
In the Views folder should be a view file which is where the markup you provided lives.
In the Controllers folder there should be a controller file for your view, which will be a class with content looking something like this:
The bit of code which binds the DropDownList control is the line:
ViewData["FORMER_BORROWER"] = ...
Now in that SelectList the first parameter is simply an IEnumerable (in my case accessing the database) so you can use LINQ to filter that object.
Below is a trivial example where the IEnumerable I provide is create in code, but the code the is accessing you database can be used in a similar way. Not how I use a join to give only the values needed.
The code above will now only show one item in the DDL - the Cycling item due to the join and the where.
Taking your controller code, you would end up with something like the code below (only focusing on one half of the FORMER_BORROWER if/else branch)
One comment on the answer above is that while this will work, I would personally look at tweaking the design, creating some sort of service for the controller to call, abstracting away some of this data access logic. Ideally you should now have your ObjectContext up in your controller layer but down in the service layer, giving a more focussed controller that is not trying to control the view at the same time as accessing the data layer.