2017-11-24 10 views
-1

私は単純なデータベースに簡単な検索機能を追加したいと思います。データベースへの簡易検索/フィルタ機能(ASP.NET MVC)(SQL Server 2014)

追加を完了しました。&削除機能を編集しました。私は検索機能を扱いにくいです。

現在、私は検索ボックスを持っており、データベースで実行する適切なSQLクエリを考えているので、簡単な結果が得られます。

私はテキストボックスフィールドをコントローラに渡して、SQLデータベース内の検索クエリとして使用して検索結果のビューに戻すことができない場合があります。

私はASP.NET MVC初心者です。助けてください。どうもありがとうございました!ここで

が私の仕事です、これまで:

ビュー

@model IEnumerable<SampleReg.Models.Course> 

@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 

@using (Html.BeginForm("Search", "Course", FormMethod.Post)) 
{ 
    <input type="text" name="txtsearch" value=" " /> 
    <input type="submit" name="btnsubmit" value="submit" /> 
} 
<table> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.crs_ID) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.crs_Course) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.crs_Major) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.crs_Spec) 
     </th> 
     <th></th> 
    </tr> 

    @foreach (var item in Model) 
    { 
     <tr> 
      <td> 
       @Html.DisplayFor(modelItem => item.crs_ID) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.crs_Course) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.crs_Major) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.crs_Spec) 
      </td> 
      <td> 
       @Html.ActionLink("Edit", "Edit", new { id = item.crs_ID }) | 
       @Html.ActionLink("Details", "Details", new { id = item.crs_ID }) | 
       @Html.ActionLink("Delete", "Delete", new { id = item.crs_ID }) 
      </td> 
     </tr> 
    } 

</table> 

コントローラ

public ActionResult Index() 
{ 
    return View(GetAllCourse("")); 
} 

[HttpPost, ActionName("Search")] 
[ValidateAntiForgeryToken] 
public ActionResult Search(string GetAllCourse) { 
string search = Request.Form["txtsearch"].ToString(); 
//GetAllCourse("search"); 
    return View(); 
} 

#region PRIVATE FUNCTIONS 
private List<Course> GetAllCourse(string search) 
{ 
    //DECLARATION AND INIT 
    SqlConnection oCon = null; 
    SqlCommand oCmd = null; 
    SqlDataReader oDr = null; 
    List<Course> oList = null; 
    string SqlCon = @"Data Source=IT-MARICOR\LOCAL;Initial Catalog=INTERN;User ID=sa;Password=aaa"; 

    try 
    { 
     //SET CONNECTION 
     oCon = new SqlConnection(); 
     oCon.ConnectionString = SqlCon; 
     if (oCon.State == System.Data.ConnectionState.Closed) 
     { 
      oCon.Open(); 
     } 

     //SET COMMAND 
     oCmd = new SqlCommand(); 
     oCmd.Connection = oCon; 
     oCmd.CommandType = System.Data.CommandType.Text; 
     oCmd.CommandText = "SELECT * FROM Course " + (search == "" ? "" : " WHERE crs_Course LIKE '% " + search + "%'"); 

     oDr = oCmd.ExecuteReader(); 

     if (oDr.HasRows) 
     { 
      oList = new List<Course>(); 

      while (oDr.Read()) 
      { 
       Course oCourse = new Course(); 
       oCourse.crs_ID = Convert.ToInt32(oDr["crs_ID"].ToString()); 
       oCourse.crs_Course = oDr["crs_Course"].ToString(); 
       oCourse.crs_Major = oDr["crs_Major"].ToString(); 
       oCourse.crs_Spec = oDr["crs_Specialization"].ToString(); 
       oList.Add(oCourse); 
      } 

      return oList; 
     } 
     return null; 
    } 
    catch (Exception ex) 
    { 
     throw ex; 
    } 
    finally 
    { 
     //CLEAN UP RESOURCES 
     oCon.Close(); 
     oCon = null; 
    } 

} 
#endregion 

ここでは、次のようになります。 enter image description here

+1

フォームコントロールは非常にあなたの方法は '公共のActionResult検索(文字列txtsearch){' –

+1

'公共のActionResult検索(文字列GetAllCourse)' =>でなければなりません ''名前は= "txtsearch" を持っている私は、これがないと思います理にかなっている。 'Request.Form [" txtsearch "]'をviewmodelと共に使うのはなぜですか?(別のプロパティと 'TextBoxFor'で十分でしょうか?) –

+1

この方法でうまくいくクライアントサイドグリッドを使用してください。https://datatables.net/ – Saineshwar

答えて

0

またはjquery ajaxメソッドの助けを借りてください。 テキストボックスにIDを1つ与えます。

var Url = "/Course/Search", 
var textvalue= $('#textboxid').val(), 
$.ajax({ 
    url: Url , 
    data: { GetAllCourse: textvalue}, 
    type: "GET", 
    url: Path, 
    success: function (result) { 

    } 
}); 
+0

私はそれをうまく修正しました。どうもありがとうございました!!! =) – Kenny

関連する問題