本文介绍了如何在显示信息列表的强类型视图中添加搜索功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个带剃须刀引擎的MVC 4应用程序。我想在我的视图中添加一个搜索功能,其中显示了一个教师列表。该视图是强类型视图。 我的模型是:Teacher.cs 命名空间 TestGrid.Models { public class 教师 { 私人 字符串 _TeacherName; private int _TeacherID,_StdID; private string ConString = @ 数据源= ztpl-dt-0001 \;初始目录= TestSanket;用户ID = sa;密码= zenopsys123; DataSet ds; public string TeacherName { set {_TeacherName = value ; } get { return _TeacherName; } } public int TeacherID { set {_TeacherID = value ; } get { return _TeacherID; } } public int StdID { set {_StdID = value ; } get { return _StdID; } } public DataTable GetTeacherList() { ds = new DataSet(); SqlConnection con = new SqlConnection(ConString); SqlDataAdapter sda = new SqlDataAdapter( select TeacherId,TeacherName,StandardId from Teacher,con); 尝试 { sda.Fill(ds); return ds.Tables [ 0 ]; } catch { return 空; } } public DataTable GetTeacher( int TeacherId) { ds = new DataSet(); SqlConnection con = new SqlConnection(ConString); SqlDataAdapter sda = new SqlDataAdapter( select TeacherId,TeacherName,StandardId from Teacher,TeacherId = + TeacherId,con); 尝试 { sda.Fill(ds); return ds.Tables [ 0 ]; } catch { return 空; } } } } 我的控制器是HomeController.cs namespace TestGrid.Controllers { public class HomeController:Controller { // // 获取:/ Home / public ActionResult Index() { return View(); } public ActionResult TeacherList() { DataTable dt = new DataTable(); Models.Teachers obj = new Models.Teachers(); dt = obj.GetTeacherList(); List< models.teachers>记录= 新列表< models.teachers>(); foreach (DataRow dr in dt.Rows) { Models.Teachers T = new Models.Teachers(); T.TeacherID = Convert.ToInt32(dr [ TeacherId]); T.StdID = Convert.ToInt32(dr [ StandardId]); T.TeacherName = dr [ TeacherName]。ToString(); Records.Add(T); } return 查看(记录); } public ActionResult TeacherDetails( int Id) { DataTable dt = new DataTable(); Models.Teachers obj = new Models.Teachers(); dt = obj.GetTeacher(Id); List< models.teachers>记录= 新列表< models.teachers>(); Models.Teachers Teacher = new Models.Teachers(); Teacher.TeacherID = Convert.ToInt32(dt.Rows [ 0 ] [ TeacherId]); Teacher.TeacherName = dt.Rows [ 0 ] [ TeacherName]的ToString(); Teacher.StdID = Convert.ToInt32(dt.Rows [ 0 ] [ StandardID]的ToString()); Record.Add(老师); return 查看(记录[ 0 ]); } public ActionResult EditList( int Id) { DataTable dt = new DataTable(); Models.Teachers obj = new Models.Teachers(); dt = obj.GetTeacher(Id); List< models.teachers>记录= 新列表< models.teachers>(); Models.Teachers Teacher = new Models.Teachers(); Teacher.TeacherID = Convert.ToInt32(dt.Rows [ 0 ] [ TeacherId]); Teacher.TeacherName = dt.Rows [ 0 ] [ TeacherName]的ToString(); Teacher.StdID = Convert.ToInt32(dt.Rows [ 0 ] [ StandardID]的ToString()); Record.Add(老师); return 查看(记录[ 0 ]); } } } 我的强类型视图显示教师名单的是:TeacherList.cshtml @ model IEnumerable< testgrid.models.teachers> @ { ViewBag.Title = TeacherList; } TeacherList < script type = text / javascript> $(function(){ $('#edit')。click(function(){ var name = $('#Name')。val(); this.href = this.href +'?Name ='+ encodeURIComponent(name); }); }); < / script> @ Html.ActionLink(创建新,创建) < input id =Nametype =textvalue =/> @ Html.ActionLink(Search,TeacherList ,null,new {Name =edit}); @foreach(模型中的var项目) { } @ Html.DisplayNameFor( model => model.TeacherName) @ Html.DisplayNameFor(model => model.TeacherID) @ Html.DisplayNameFor(model => model.StdID) @ Html.DisplayFor(modelItem => item.TeacherName) @ Html.DisplayFor(modelItem => item.TeacherID) @ Html.DisplayFor(modelItem => item。 StdID) @ Html.ActionLink(编辑,编辑列表,新{id = item.TeacherID})| @Html .ActionLink(详细信息,教师详细信息,新{id = item.TeacherID})| @ Html.ActionLink(删除,删除,新{/ * id =项目。 PrimaryKey * /}) Plss给我建议如何添加文本框以接受用户输入并搜索单个记录并在此视图中显示它。解决方案 (function(){ ('#edit')。点击( function(){ var name = ('#Name')。val(); this.href = this .href +'?Name ='+ encodeURIComponent(name); }); }); < / script> b $ b @ Html.ActionLink(新建,创建) < input id =Nametype =textvalue =/ > @ Html.ActionLink(搜索,教师列表,空,新{名称=编辑}); @foreach(型号中的var项目) { } @ Html.DisplayNameFor(model => model.TeacherName) @ Html.DisplayNameFor(model => model.TeacherID) @Html。 DisplayNameFor(model => model.StdID) @ Html.DisplayFor(modelItem => item。 TeacherName) @ Html.DisplayFor(modelItem => item.TeacherID) @ Html.DisplayFor( modelItem => item.StdID) @ Html.ActionLink(编辑,编辑列表,新{id = item.TeacherID})| @ Html.ActionLink(详细信息,TeacherDetails,新{id = item.TeacherID})| @ Html.ActionLink(删除,删除,新{ / * id = item.PrimaryKey * /}) Plss给我建议如何添加文本框到acc ept用户输入并搜索单个记录并在此视图中显示它。 I have a MVC 4 application with razor engine. I want to add a search function to my view which show a list of teachers. The view is strongly typed view.My Models is : Teacher.csnamespace TestGrid.Models{ public class Teachers { private String _TeacherName; private int _TeacherID, _StdID; private string ConString = @"Data Source=ztpl-dt-0001\;Initial Catalog=TestSanket;User ID=sa;Password=zenopsys123"; DataSet ds; public string TeacherName { set { _TeacherName = value; } get { return _TeacherName; } } public int TeacherID { set { _TeacherID = value; } get { return _TeacherID; } } public int StdID { set { _StdID = value; } get { return _StdID; } } public DataTable GetTeacherList() { ds = new DataSet(); SqlConnection con = new SqlConnection(ConString); SqlDataAdapter sda = new SqlDataAdapter("select TeacherId,TeacherName,StandardId from Teacher", con); try { sda.Fill(ds); return ds.Tables[0]; } catch { return null; } } public DataTable GetTeacher(int TeacherId) { ds = new DataSet(); SqlConnection con = new SqlConnection(ConString); SqlDataAdapter sda = new SqlDataAdapter("select TeacherId,TeacherName,StandardId from Teacher where TeacherId = " + TeacherId, con); try { sda.Fill(ds); return ds.Tables[0]; } catch { return null; } } }}My Controller is HomeController.csnamespace TestGrid.Controllers{ public class HomeController : Controller { // // GET: /Home/ public ActionResult Index() { return View(); } public ActionResult TeacherList() { DataTable dt = new DataTable(); Models.Teachers obj = new Models.Teachers(); dt = obj.GetTeacherList(); List<models.teachers> Records = new List<models.teachers>(); foreach(DataRow dr in dt.Rows) { Models.Teachers T=new Models.Teachers(); T.TeacherID = Convert.ToInt32(dr["TeacherId"]); T.StdID = Convert.ToInt32(dr["StandardId"]); T.TeacherName = dr["TeacherName"].ToString(); Records.Add(T); } return View(Records); } public ActionResult TeacherDetails(int Id) { DataTable dt = new DataTable(); Models.Teachers obj = new Models.Teachers(); dt = obj.GetTeacher(Id); List<models.teachers> Record = new List<models.teachers>(); Models.Teachers Teacher = new Models.Teachers(); Teacher.TeacherID = Convert.ToInt32(dt.Rows[0]["TeacherId"]); Teacher.TeacherName = dt.Rows[0]["TeacherName"].ToString(); Teacher.StdID = Convert.ToInt32(dt.Rows[0]["StandardID"].ToString()); Record.Add(Teacher); return View(Record[0]); } public ActionResult EditList(int Id) { DataTable dt = new DataTable(); Models.Teachers obj = new Models.Teachers(); dt = obj.GetTeacher(Id); List<models.teachers> Record = new List<models.teachers>(); Models.Teachers Teacher = new Models.Teachers(); Teacher.TeacherID = Convert.ToInt32(dt.Rows[0]["TeacherId"]); Teacher.TeacherName = dt.Rows[0]["TeacherName"].ToString(); Teacher.StdID = Convert.ToInt32(dt.Rows[0]["StandardID"].ToString()); Record.Add(Teacher); return View(Record[0]); } }}My strongly typed view which displays list of teachers is: TeacherList.cshtml@model IEnumerable<testgrid.models.teachers>@{ ViewBag.Title = "TeacherList";}TeacherList<script type="text/javascript"> $(function () { $('#edit').click(function () { var name = $('#Name').val(); this.href = this.href + '?Name=' + encodeURIComponent(name); }); });</script> @Html.ActionLink("Create New", "Create")<input id="Name" type="text" value="" />@Html.ActionLink("Search", "TeacherList", null, new { Name = "edit" }); @foreach (var item in Model){ } @Html.DisplayNameFor(model => model.TeacherName) @Html.DisplayNameFor(model => model.TeacherID) @Html.DisplayNameFor(model => model.StdID) @Html.DisplayFor(modelItem => item.TeacherName) @Html.DisplayFor(modelItem => item.TeacherID) @Html.DisplayFor(modelItem => item.StdID) @Html.ActionLink("Edit", "EditList", new { id = item.TeacherID }) | @Html.ActionLink("Details", "TeacherDetails", new { id = item.TeacherID }) | @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })Plss give me suggestion how to add a textbox to accept user input and search the single record and show it in this view itself. 解决方案 (function () {('#edit').click(function () { var name =('#Name').val(); this.href = this.href + '?Name=' + encodeURIComponent(name); }); });</script> @Html.ActionLink("Create New", "Create")<input id="Name" type="text" value="" />@Html.ActionLink("Search", "TeacherList", null, new { Name = "edit" }); @foreach (var item in Model){ } @Html.DisplayNameFor(model => model.TeacherName) @Html.DisplayNameFor(model => model.TeacherID) @Html.DisplayNameFor(model => model.StdID) @Html.DisplayFor(modelItem => item.TeacherName) @Html.DisplayFor(modelItem => item.TeacherID) @Html.DisplayFor(modelItem => item.StdID) @Html.ActionLink("Edit", "EditList", new { id = item.TeacherID }) | @Html.ActionLink("Details", "TeacherDetails", new { id = item.TeacherID }) | @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })Plss give me suggestion how to add a textbox to accept user input and search the single record and show it in this view itself. 这篇关于如何在显示信息列表的强类型视图中添加搜索功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-09 09:33