私はそれが非常によく動作しますが、このようなコードを試してみました:複数のフィールド/列でデータを検索する方法は?
ビュー:
@model IEnumerable<InternProject.Models.Course>
....
@Html.ActionLink("Create New", "Create")
@using (Html.BeginForm("Index", "Course", FormMethod.Get))
{
<p>
@Html.TextBox("searching")
<input type="submit" value="search" />
</p>
}
<table class="table table-striped">
<thead>
<tr>
<th>Course ID</th>
<th>Course Name</th>
<th>Major</th>
<th>Specialization</th>
<th></th>
</tr>
</thead>
@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_Specialization)</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 class CourseController : Controller
{
private DbCourse db = new DbCourse();
public ActionResult Index(string submit, string searching)
{
var course = from x in db.Course select x;
if (!String.IsNullOrWhiteSpace(searching))
{
return View(db.Course.Where(x => x.crs_Course.Contains(searching.Trim()) ||
x.crs_Major.Contains(searching.Trim()) ||
x.crs_Specialization.Contains(searching.Trim())).ToList());
}
else if (searching == null)
{
return View(db.Course.Where(x => x.crs_Course == searching || searching.Trim() == null).ToList());
}
else
{
return View(db.Course.ToList());
}
}
}
しかし、それはですのでid
を含めることはできませんが整数。私は検索ボックスの入力に応じて私のデータベースのid
でも検索できる解決策が必要です。
また、このような単純な検索機能では、これよりも優れたコードがありますか?私はそれがとても長いことに気付きました、そして、明らかにDRYの原則に違反しています。
私は初心者としてASP.NET MVCで私の赤ちゃんのステップを取っている:ここで
は私の単純なアプリケーションがどのように見えるかです。
私はチュートリアルのビデオに頼るだけでなく、適用されたコーディングを使用して自分の知識を向上させたいと考えています。
ありがとうございます! =)
なぜ簡単に検索できません: Convert.ToInt32(searching.Trim()) ? – FaizanRabbani
なぜ各列のテキストボックスはありませんか? –
また、 'else if(searching == null)'ブロックのコードは意味をなさない。 –