0

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

管理员:
未分配
未分配
"
"
"
Alexandra M.
Martin D.
Alexandra M.

我希望它看起来像:

管理员:
未分配
Martin 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>
4

2 回答 2

1

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

这将为您提供下拉列表的不同值,否则您将不得不从返回的结果中执行此操作。

于 2013-06-18T19:45:13.387 回答
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

于 2013-06-18T20:23:05.010 回答