使用非重复值填充下拉列表

时间:2013-06-18 18:57:01

标签: sql drop-down-menu webmatrix

我有一个多重搜索查询,其中包含一个下拉菜单,用于选择需要从我的RMS数据库中的Assigned_To列调用的管理员。问题是下拉列表显示重复的每个值。例如:

管理
未分配
未分配



Alexandra M.
马丁D.
Alexandra M.

我希望它看起来像:

管理
未分配
马丁D.
Alexandra M.

“未分配”是创建的案例的默认值。

这是我的代码:

var db = Database.Open("RMS") ;

var selectCommand = "SELECT * FROM RMS";
var formSSO = "";
var formAssigned_To = "";
var formCase_Status= "";
var fromDate = "";
var toDate = "";

    formSSO = Request.QueryString["formSSO"];
    formAssigned_To = "%" + Request["formAssigned_To"] + "%";
    formCase_Status = Request["formCase_Status"];
    fromDate = Request.QueryString["fromDate"];
    toDate = Request.QueryString["toDate"];

    selectCommand = "SELECT DISTINCT * FROM RMS WHERE 1=1";

if(!Request.QueryString["formSSO"].IsEmpty() ) {
     selectCommand +=" AND SSO LIKE @0";
}

if(!Request.QueryString["formAssigned_To"].IsEmpty() ) {
     selectCommand +=" AND Assigned_To LIKE @1";
}

if(!Request.QueryString["formCase_Status"].IsEmpty() ) {
     selectCommand +=" AND Case_Status = @2";
}

if(!Request.QueryString["fromDate"].IsEmpty() ) {
     selectCommand +=" AND Created >= @3";
}


if(!Request.QueryString["toDate"].IsEmpty() ) {
     selectCommand +=" AND Created <= @4";
}



var data = db.Query(selectCommand, formSSO, formAssigned_To, formCase_Status, fromDate, toDate);
var columns = new[]{"ID", "SSO", "Category", "System", "Subject", "Created", "Assigned_To"};
var grid = new WebGrid(data, ajaxUpdateContainerId: "grid", defaultSort: "ID", columnNames: columns);
if (Request.QueryString[grid.SortDirectionFieldName].IsEmpty()) {
grid.SortDirection = SortDirection.Descending;
}
}

这是下拉列表的html表单字段:

<li class="form-line form-line-column" id="id_22">
<label class="form-label-top" id="label_22" for="input_22"> ADMIN </label>
<div id="cid_22" class="form-input-wide">       
<select name="formAssigned_To">
 @foreach(var row in data){
        <option></option>
        <option value="@row.ID">@row.Assigned_To</option>
    }
    </select>
    </div>
    </li>

2 个答案:

答案 0 :(得分:1)

你不能只使用     SELECT DISTINCT Assigned_To FROM RMS WHERE 1=1

这会为您提供下拉列表的不同值,否则您必须这样做 从返回的结果。

答案 1 :(得分:0)

我明白了!我将表单字段更改为:(它有自己的SELECT DISTINCT,因为它特定于此字段。

       <li class="form-line form-line-column" id="id_22">
        <label class="form-label-top" id="label_22" for="input_22"> ADMIN </label>
        <div id="cid_22" class="form-input-wide">       
        <select name="formAssigned_To">
        <option></option>
        @foreach(var row in db.Query("SELECT DISTINCT Assigned_To FROM RMS")){
        <option value="@row.Assigned_To">@row.Assigned_To</option>
         }
        </select>
        </div>
        </li>

我离开了我的selectCommand:

selectCommand = "SELECT * FROM RMS WHERE 1=1";

这要归功于:CSHTML - SQL QUERY....WHERE NAME=@NameOne