如何将下拉列表中的选项限制为仅有效选项?

时间:2011-01-26 21:32:04

标签: c# asp.net asp.net-mvc-2

请允许我进一步解释,我的数据库中有很多人。他们有一个多选项来制定行动计划,我的页面会显示所有有行动计划的人。我也有下拉列表来过滤掉所有有行动计划的人......这很好。下拉列表工作正常,我无法得到的部分是有一个带有人名的下拉列表过滤器(找到该人的所有行动计划),但是下拉列表显示数据库中的所有名称而不仅仅是有行动计划,有没有办法限制下拉列表只显示有行动计划的人?

对不起,如果已经发布了这个帖子......我一直在寻找并寻找但无法找到解决方案

感谢

P.S。这可能会有所帮助......是的,我正在使用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");
    }

2 个答案:

答案 0 :(得分:1)

这更像是一个Sql问题,基本上你希望你的下拉列表显示这个请求的结果:

伪代码:

Select Person.* from Person Inner Join ActionPlan on Person.PersonnId = ActionPlan.PersonId

内部联接使您的查询仅返回表人员中具有ActionPlan表中记录的行。

答案 1 :(得分:0)

在Html.DropDownList上做一些快速的Google搜索似乎指出这是ASP.Net MVC - 这是正确的吗?

如果你正在使用MVC我很惊讶你所做的就是在屏幕上显示数据库数据而不知道你可以在哪里过滤那些数据。你到目前为止自动生成了一切吗?

我没有非常使用MVC,只是在Visual Studio中组合了一个快速而肮脏的示例。您应该看到两个文件夹,一个名为Views,另一个名为Controllers。

在Views文件夹中应该是一个视图文件,它是您提供的标记所在的位置。

在Controllers文件夹中,您的视图应该有一个控制器文件,该文件的内容类似于:

namespace MvcApplication1.Controllers
{
    [HandleError]
    public class YourController : Controller
    {
        public ActionResult YourView()
        {
            ViewData["FORMER_BORROWER"] = new SelectList(db.Borrowers.ToList, "BorrowerID, "Name");               

            return View();
        }
    }
}

绑定DropDownList控件的代码行是:     ViewData [“FORMER_BORROWER”] = ...

现在在SelectList中,第一个参数只是一个IEnumerable(在我的情况下访问数据库),因此您可以使用LINQ来过滤该对象。

下面是一个简单的例子,其中我提供的IEnumerable是在代码中创建的,但是访问数据库的代码可以以类似的方式使用。不是我如何使用连接来仅提供所需的值。

List<SelectListItem> items = new List<SelectListItem>(); 
            items.Add(new SelectListItem { Text = "Swimming", Value = "1" }); 
            items.Add(new SelectListItem { Text = "Cycling", Value = "2", Selected = true }); 
            items.Add(new SelectListItem { Text = "Running", Value = "3" });         

List<ExerciseType> et = new List<ExerciseType>();
    et.Add(new ExerciseType{Id="1"});
    et.Add(new ExerciseType{Id="2"});

// the line below uses LINQ to filter out the item with text of "Swimming"            
var filteredList = from x in items
                    join y in et on x.Value equals y.Id
                    where x.Text != "Swimming"
                    select x;


ViewData["Testing"] = new SelectList(filteredList, "Value", "Text");

上面的代码现在只显示DDL中的一个项目 - 由于连接和其中的循环项目。

拿你的控制器代码,你会得到类似下面代码的东西(只关注FORMER_BORROWER if / else分支的一半)

FormerBorrower = Request.Form["FORMER_BORROWER"];            
ViewData["FORMER_BORROWER"] = new SelectList((
    from n in _db.Loans
    join x in _db.ActionPlans on n.BorrowerId equals x.BorrowerId
    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);       

上面答案的一个评论是,虽然这可行,但我个人会考虑调整设计,为控制器创建某种服务来调用,抽象掉一些数据访问逻辑。理想情况下,您现在应该在控制器层中将ObjectContext放在服务层中,然后放在服务层中,从而提供一个更集中的控制器,它不会在访问数据层的同时控制视图。