Asp.net mvc 4 ASP.NET MVC中的搜索功能工作不正常

Asp.net mvc 4 ASP.NET MVC中的搜索功能工作不正常,asp.net-mvc-4,Asp.net Mvc 4,我在我创建的数据库中有一个学生表,我有一个视图,显示按班级分组的所有学生的列表。。。在视图的顶部,我制作了一个文本框和一个搜索按钮,以便能够更快地访问学生信息。问题是,当我在文本框中输入名字和姓氏时,什么都不会出现。当我只输入名字或姓氏时,它就会找到它。我是编程新手,我不知道如何让它工作。如果有人能帮我,我将不胜感激。这是我代码的一部分: [HttpGet] public ActionResult ViewStudents() { ViewBag.classes = db.Courses

我在我创建的数据库中有一个学生表,我有一个视图,显示按班级分组的所有学生的列表。。。在视图的顶部,我制作了一个文本框和一个搜索按钮,以便能够更快地访问学生信息。问题是,当我在文本框中输入名字和姓氏时,什么都不会出现。当我只输入名字或姓氏时,它就会找到它。我是编程新手,我不知道如何让它工作。如果有人能帮我,我将不胜感激。这是我代码的一部分:

[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);
 }
[HttpGet]
公众行动结果学生()
{
ViewBag.classes=db.Courses.ToList();
var studentCourses=db.studentCourses.OrderBy(s=>s.Person.FirstName.ToList();
返回视图(学生课程);
}
[HttpPost]
公共操作结果视图学生(字符串搜索字符串)
{
var student=新列表();
Listsc=新列表();
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();
如果(!string.IsNullOrEmpty(SearchString))
{
学生=(来自数据库中的p.People)
其中(p.FirstName.Contains(SearchString))&(p.LastName.Contains(substring))| |((p.FirstName.Contains(SearchString))| |(p.LastName.Contains(SearchString)))
选择p.PersonId).ToList();
}
foreach(学生课程中的var s)
{
foreach(学生中的var i)
{
如果(s.StudentId==i)
{
sc.Add(s);
}
}
}
返回视图(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; }
}
}
@型号列表
@使用(Html.BeginForm())
{
}
@{
列表c=新列表();
foreach(ViewBag.classes中的var课程)
{
foreach(模型中的var s)
{
if(courses.CourseId==s.CourseId)
{
c、 添加(courses.CourseId);
}
}
}
}
@foreach(ViewBag.classes中的var课程)
{  
if(c.Contains(course.CourseId))
{
@course.Name--@course.Gender
名字姓氏电子邮件电话号码着装出生日期
@foreach(模型中的var s)
{
if(course.CourseId==s.CourseId)
{
@s、 人名
@s、 人名
@s、 个人1.电子邮件
@s、 个人电话号码
@s、 地址
@s、 人的出生日期
@ActionLink(“编辑”、“编辑”、“人”,新的{id=s.Person1.PersonId},空)|
@ActionLink(“详细信息”,“详细信息”,“个人”,新的{id=s.Person1.PersonId},空)
}
}
}
}
这是我的个人模型:
公共部分阶级人士
{
公众人士()
{
this.Bonus=newhashset();
this.ConversationHistories=新HashSet();
this.ConversationHistories1=新的HashSet();
this.EmployeePaymentDetails=new HashSet();
this.StudentCourses=new HashSet();
this.StudentCourses1=新HashSet();
this.TeacherCourses=new HashSet();
this.remements=newhashset();
}
公共int PersonId{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串用户名{get;set;}
公共字符串电子邮件{get;set;}
公共字符串PhoneNumber{get;set;}
公共字符串地址{get;set;}
公共可为空的出生日期{get;set;}
公共PersonType PersonTypeId{get;set;}
公共可为空的LastModified{get;set;}
公共可空性别{get;set;}
公共可空状态{get;set;}
公共字符串全名
{
获取{return FirstName+“,“+LastName;}
}
公共虚拟ICollection奖金{get;set;}
公共虚拟ICollection会话历史记录{get;set;}
公共虚拟ICollection会话历史1{get;set;}
公共虚拟ICollection EmployeePaymentDetails{get;set;}
公共虚拟ICollection学生课程{get;set;}
公共虚拟ICollection学生课程1{get;set;}
公共虚拟ICollection教师课程{get;set;}
公共虚拟ICollection提醒{get;set;}
}
}

您可能希望尝试将人名模型中的名字和姓氏属性连接起来,如下所示:

    [Display(Name = "Full Name")]
    public string FullName
    {
        get
        {
            return LastName + ", " + FirstMidName;
        }
    }
这里有一个非常好的教程,介绍您要做的事情:

另请参见同一教程的本页:


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

谢谢您的帮助。我在我的个人模型中添加了这个,我应该在我的动作方法中添加什么?你能看一下我的代码并详细地告诉我应该修改什么以及如何修改吗。谢谢我的ViewModel应该是什么样子的?我自己也是新手,但我会尝试使用一个包含名字和姓氏的连接的属性,然后根据它进行搜索。例如:if(!String.IsNullOrEmpty(searchString)){students=students.Where(s=>s.FullName.Contains(searchString)};var people=from p in db.people选择p;if(!String.IsNullOrEmpty(searchString)){people=people.Where(p=>p.FullName.Contains)(搜索字符串);}