ASP.NET MVC中的搜索功能无法正常工作

时间:2016-11-13 18:33:46

标签: asp.net-mvc-4

我在我创建的数据库中有一个学生表,我有一个视图,显示按类分组的所有学生的列表...在视图的顶部我创建了一个文本框和一个搜索按钮,以便能够更快地访问学生信息。问题是,当我输入文本框中的名字和姓氏时,没有任何内容出现。当我只输入名字或只输入姓氏时,它会找到它。我是编程新手,我无法弄清楚如何使它工作。如果有人可以帮助我,我真的很感激。这是我的代码的一部分:

[HttpGet]
public ActionResult ViewStudents()
{
    ViewBag.classes = db.Courses.ToList();
    var studentCourses = db.StudentCourses.OrderBy(s=>s.Person.FirstName).ToList();

      return View(studentCourses);
}

[HttpPost]
public ActionResult ViewStudents(string SearchString)
{
   var student=new List<int>();
   List<StudentCourse>sc=new List<StudentCourse>();
   ViewBag.classes = db.Courses.ToList();
   var studentCourse=db.StudentCourses.ToList();
   var studentCourses = db.StudentCourses.OrderBy(s => s.Person.FirstName).ToList();
   var substring = SearchString.IndexOf(" ").ToString();
   if (!string.IsNullOrEmpty(SearchString))
   {
       student = (from p in db.People
                  where (p.FirstName.Contains(SearchString)) && (p.LastName.Contains(substring))||((p.FirstName.Contains(SearchString)) || (p.LastName.Contains(SearchString)))
                  select p.PersonId).ToList();
   }
   foreach (var s in studentCourse)
   {
       foreach (var i in student)
       {
           if (s.StudentId == i)
           {
               sc.Add(s);
           }
       }
   }
   return View(sc);
 }

这是我的观点:

@model List<SchoolFinalProject.Models.StudentCourse>
@using (Html.BeginForm())
{
    <div style="font-size:16px;"> <input type="text" id="search" placeholder="search" Name="SearchString" /><span class="glyphicon glyphicon-search"></span>
    <input type="submit" value="search"></div>
}
@{
    List<int> c = new List<int>();
    foreach (var courses in ViewBag.classes)
    {
        foreach(var s in Model)
        {
            if(courses.CourseId==s.CourseId)
            {
                c.Add(courses.CourseId);
            }
        }
    }
}

@foreach (var course in ViewBag.classes)
{  
    if(c.Contains(course.CourseId))
    {
        <h2>@course.Name<span>-</span>@course.Gender</h2>
        <table class="table table-hover table-bordered table-striped">
           <tr><th>First Name</th><th>Last Name</th><th>Email</th><th>Phone Number</th><th>Address</th><th>Date Of Birth</th></tr>
           @foreach (var s in Model)
           {
               if(course.CourseId==s.CourseId)
               {
                   <tr>
                       <td>@s.Person1.FirstName</td>
                       <td>@s.Person1.LastName</td>
                       <td>@s.Person1.Email</td>
                       <td>@s.Person1.PhoneNumber</td>
                       <td>@s.Person1.Address</td>
                       <td>@s.Person1.DateOfBirth</td>
                       <td>
                           <span class="glyphicon glyphicon-edit"></span>
                           @Html.ActionLink("Edit", "Edit","Person", new { id = s.Person1.PersonId }, null) |
                           <span class="glyphicon glyphicon-trash"></span>
                           @Html.ActionLink("Details", "Details","Person", new { id = s.Person1.PersonId }, null)
                       </td>
                   </tr>
               }
           }
       </table>
    }
  }

<a href="#top" title="Go to top of page">Go to top of page</a>
this is my person Model:
public partial class Person
{
    public Person()
    {
        this.Bonus = new HashSet<Bonu>();
        this.ConversationHistories = new HashSet<ConversationHistory>();
        this.ConversationHistories1 = new HashSet<ConversationHistory>();
        this.EmployeePaymentDetails = new HashSet<EmployeePaymentDetail>();
        this.StudentCourses = new HashSet<StudentCourse>();
        this.StudentCourses1 = new HashSet<StudentCourse>();
        this.TeacherCourses = new HashSet<TeacherCourse>();
        this.Reminders = new HashSet<Reminder>();
    }

    public int PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }
    public string Address { get; set; }
    public Nullable<System.DateTime> DateOfBirth { get; set; }
    public PersonType PersonTypeId { get; set; }
    public Nullable<System.DateTime> LastModified { get; set; }
    public Nullable<int> Gender { get; set; }
    public Nullable<int> Status { get; set; }
    public string FullName
    {
        get { return FirstName + ", " + LastName; }
    }
    public virtual ICollection<Bonu> Bonus { get; set; }
    public virtual ICollection<ConversationHistory> ConversationHistories { get; set; }
    public virtual ICollection<ConversationHistory> ConversationHistories1 { get; set; }
    public virtual ICollection<EmployeePaymentDetail> EmployeePaymentDetails { get; set; }
    public virtual ICollection<StudentCourse> StudentCourses { get; set; }
    public virtual ICollection<StudentCourse> StudentCourses1 { get; set; }
    public virtual ICollection<TeacherCourse> TeacherCourses { get; set; }
    public virtual ICollection<Reminder> Reminders { get; set; }
}
}

1 个答案:

答案 0 :(得分:1)

您可能想尝试在人员模型中连接名字和姓氏属性,如下所示:

    [Display(Name = "Full Name")]
    public string FullName
    {
        get
        {
            return LastName + ", " + FirstMidName;
        }
    }

有一个很好的教程,说明你要在这里做什么:https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp-net-mvc-application

另请参阅相同教程的此页面:https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

另外,您可能希望使用Datatables插件进行检查,该插件为您提供搜索功能,而无需在每次搜索时查询您的数据库:https://datatables.net