如何使用cshtml razor将多个文本框变量添加到一个sql select查询中?

时间:2015-05-27 16:22:13

标签: jquery asp.net-mvc-4 razor webmatrix webmatrix-3

目前在下面的代码中,我有两个搜索按钮,只能查询每个框中的内容。我想填写两个框并点击一个提交按钮,它会查询两个变量。像

这样的东西
 {if(!Request.QueryString["searchDateIn"].IsEmpty() ) 
  selectCommand = "SELECT * FROM Data_TimeCards_T WHERE Employeeid = @0 
   and datein = @1";
    searchTerm = Request.QueryString["searchEmployee"];}

我的代码现在看起来像这样

var db = Database.Open("datbasename") ;
var selectCommand = "SELECT * FROM Data_timecards_T";
var searchTerm = "";

if(!Request.QueryString["searchEmployee"].IsEmpty() ) {
    selectCommand = "SELECT * FROM Data_TimeCards_T WHERE Employeeid = @0";
    searchTerm = Request.QueryString["searchEmployee"];
}

if(!Request.QueryString["searchDateIn"].IsEmpty() ) {
    selectCommand = "SELECT * FROM Data_timeCards_t WHERE Datein = @0";
    searchTerm = Request.QueryString["searchDateIn"] ;
}

var selectedData = db.Query(selectCommand, searchTerm);
var grid = new WebGrid(source: selectedData, defaultSort: "EmployeeID", rowsPerPage:3);

身体看起来像这样

<form method="get">
        <div>
        <label for="searchEmployee">Employee to look for:</label>
        <input type="text" name="searchEmployee" value="@Request.QueryString["searchEmployee"]" />
        <input type="Submit" value="Search Employee" /><br/>
        (Leave blank to list all movies.)<br/>
        </div>

        <div>
          <label for="searchDateIn">DateIn contains the following:</label>
          <input type="text" name="searchDateIn" value="@Request.QueryString["searchDateIn"]" />
          <input type="Submit" value="Search Date In" /><br/>
        </div>
      </form>

    <div>
      @grid.GetHtml(
        tableStyle: "grid",
        headerStyle: "head",
        alternatingRowStyle: "alt",
        columns: grid.Columns(
          grid.Column("DateIn"),
          grid.Column("EmployeeID"),
          grid.Column("TotalTime")
        )
      )
    </div>

1 个答案:

答案 0 :(得分:0)

我能够通过将代码更改为您在下面看到的内容来解决问题

var db = Database.Open("database name") ;
var selectCommand = "SELECT * FROM Data_timecards_T";
  var searchEmployee = "";
  var searchDate = "";}

 if(!Request.QueryString["searchEmployee"].IsEmpty() ) {
        selectCommand = "SELECT * FROM Data_TimeCards_T WHERE Employeeid = @0 and Datein = @1";
        searchEmployee = Request.QueryString["searchEmployee"];
          searchDate = Request.QueryString["searchDate"];
    }
    var selectedData = db.Query(selectCommand, searchEmployee,searchDate);
    var grid = new WebGrid(source: selectedData, defaultSort: "EmployeeID", rowsPerPage:3);}

表单部分现在看起来像这样:

   {<form method="get">
        <div>
        <label for="searchEmployee">Employee to look for:</label>
        <input type="text" name="searchEmployee"   value="@Request.QueryString["searchEmployee"]" />
         <label for="searchDate">DateIn contains the following:</label>
          <input type="text" name="searchDate" value="@Request.QueryString["searchDate"]" />
          <input type="Submit" value="SearchData" /><br/>
        </div>
      </form>}