获取所有相关数据到get&amp ;;过滤发布请求

时间:2016-11-26 19:53:24

标签: asp.net asp.net-mvc entity-framework ef-code-first asp.net-mvc-viewmodel

好的我有一个ASP.NET MVC应用程序,其中我有以下三个模型

    public class Student
    {
        public int Id { get; set; }
        [Display(Name = "First Name")]
        public string FirstName { get; set; }
        [Display(Name = "Last Name")]
        public string LastName { get; set; }
        public virtual ProfilePic ProfilePic { get; set; }
        public virtual Resume Resume { get; set; }
        }

    public class ProfilePic
    {
        public int Id { get; set; }
        public string ImageName { get; set; }
        public string ImagePath { get; set; }
        public virtual ICollection<Student> Students { get; set; }
    }

    public class Resume
    {
        public int Id { get; set; }
        public string ResumeFileName { get; set; }
        public string ResumeFilePath { get; set; }
        public virtual ICollection<Student> Students { get; set; }
    }

我有一个像下面的SearchViewModel:

 public class SearchViewModel
    {
        [Required]
        [Display(Name = "Search")]
        public string Search { get; set; }
        public IEnumerable<Student> Students { get; set; }
    }

我有两个ActionResult方法,如下所示:

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Search()
{
    SearchViewModel searchViewModel = new SearchViewModel();
    // I want to add all related records here inside ViewModel &
    // return the viewmodel
    return View(searchViewModel)
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Search(SearchViewModel m)
{
   if (ModelState.IsValid)
   {
       List<Student> StudentList = db.Students.ToList();
       var getResults = StudentList;
       if (!string.IsNullOrWhiteSpace(m.Search))
       {
           var newResults = (from x in db.Students.Include("ProfilePic").Include("Resume")
                             where x.FirstName.Contains(m.Search)
                             select x).ToList();
           m.Students = newResults;
       }
  }
  return View(m);

}

我的Search.cshtml看起来像这样:

@using MyApplication.Models
@model MyApplication.Areas.Employers.Models.SearchViewModel

@{
    ViewBag.Title = "Search";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Search</h2>

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

@using (Html.BeginForm("Search", "Main", FormMethod.Post))
{

    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <div class="form-group">
            @Html.LabelFor(model => model.Search, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Search, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Search, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" class="btn btn-primary" value="Search" />
            </div>
        </div>
    </div>
}
<table>
    @foreach (Student s in Model.Students)
    {
        <tr>
            <td>
                <p>@s.FirstName</p>
            </td>
            <td>
                <p>@s.LastName</p>
            </td>
            <td>
                <p>@s.ProfilePic.ImageName</p>
            </td>
            <td>
                <p>@s.Resume.ResumeFilePath</p>
            </td>
            </tr>
    }
</table>

我想要做的是在加载页面时获取所有学生数据,然后允许用户根据搜索词(现在只是FirstName)过滤结果。

我尝试了其他解决方案,但我无法弄清楚如何在Get类型的搜索方法中将所有模型的数据添加到视图中时返回viewmodel

0 个答案:

没有答案