如何在MVC中获取视图以正确引用所需的模型

时间:2019-04-03 08:08:11

标签: c# razor model-view-controller

这是我遇到的错误

  

传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [WTCoro2.Models.Person]',但是此字典需要类型为'WTCoro2.Models.PersonViewModel'的模型项。”但是这里的问题是我正在使用'WTCoro2.Models.PersonViewModel'

我已经尝试按照错误提示进行操作,但仍然给我相同的错误。

这是我的观点:

@model WTCoro2.Models.PersonViewModel

@{
    ViewBag.Title = "Multidata";
}

<h2>Multidata</h2>
<h2>People</h2>
<div>
    @{ WebGrid obj = new WebGrid(source: Model.pers, canPage: true, rowsPerPage: 10);}
    @obj.GetHtml();
</div>
<br />

<h2>Employees</h2>
<div>
    @{ WebGrid obj1 = new WebGrid(source: Model.emp, canPage: true, rowsPerPage: 10);}
    @obj1.GetHtml();
</div>

这是我的控制人:

// GET: People
public ActionResult Multidata(string sortOrder, string searchString, string currentFilter, int? page, string searchBy, string startdate = null, string enddate = null)
{
    var mymodel = new PersonViewModel();
    ViewBag.CurrentSort = sortOrder;
    ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
    var people_list = mymodel.pers = db.People.ToList();
    var employee_list = db.Employees.ToList();
    var history_list = mymodel.history = db.EmployeeDepartmentHistories.ToList();
    /* if (searchString != null)
     {
         page = 1;
     }
     else
     {
         searchString = currentFilter;
     }
     ViewBag.CurrentFilter = searchString;*/
    searchString = "";
    if (searchBy == "Title")
    {
        return View(employee_list.Where(x => x.JobTitle == searchString || searchString == null).ToList());
    }
    /*  else if (startdate != null && enddate != null)
      {
          DateTime start = DateManager.GetDate(startdate) ?? DateTime.Now;
          DateTime end = DateManager.GetDate(enddate) ?? DateTime.Now;
          return View(history_list.Where(x => x.StartDate >= start && x.EndDate <= end).ToList());

      }*/
    else
    {
        return View(people_list.Where(x => x.FirstName.StartsWith(searchString) || x.LastName.StartsWith(searchString) || searchString == null).ToList());
    }
}

还有我的ViewModel:

public class PersonViewModel
{
    public IEnumerable <Person> pers { get; set; }
    public Person Person { get; set; }
    public IEnumerable <EmployeeDepartmentHistory> history { get; set; }
    public EmployeeDepartmentHistory EmployeeDepartmentHistory { get; set; }
    public IEnumerable <Employee> emp { get; set; }
    public Employee Employee { get; set; }
}

3 个答案:

答案 0 :(得分:0)

Yout视图需要一个PersonViewModel。但是,您正在传递一个PersonList。

在这种情况下,您应该传递PersonViewModel。

0.31336236000061035
4.291534423828125e-06
True

答案 1 :(得分:0)

您的控制器返回IEnumerable<Person>而不是PersonViewModel。在视图中进行任何更改:

@model IEnumerable<WTCoro2.Models.PersonViewModel.pers>

或更改控制器:

mymodel.pers = (people_list.Where(x => x.FirstName.StartsWith(searchString) || x.LastName.StartsWith(searchString) || searchString == null).ToList();

return View(mymodel);

另外,请使用正确的naming conventions(即大写的属性名称)。

答案 2 :(得分:0)

您要在操作方法中声明视图模型,但不会将其返回到视图中。取而代之的是,您将查询从数据库返回到视图,当然,查询的类型与视图模式不同。

因此,您要做的基本上是这样:

public ActionResult Multidata(string sortOrder, string searchString, string currentFilter, int? page, string searchBy, string startdate = null, string enddate = null)
{
    var mymodel = new PersonViewModel();
    ViewBag.CurrentSort = sortOrder;
    ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
    mymodel.pers = db.People.ToList();
    mymodel.emp = db.Employees.ToList();
    mymodel.history = db.EmployeeDepartmentHistories.ToList();
    /* ... */
    if (searchBy == "Title")
    {
        return View(mymodel.Where(x => x.emp.JobTitle == searchString || searchString == null).ToList());
    }
    else
    {
        return View(mymodel.Where(x => x.pers.FirstName.StartsWith(searchString) || x.pers.LastName.StartsWith(searchString) || searchString == null).ToList());
    }
}

关键点在这些行中:

var people_list = mymodel.pers = db.People.ToList();

您正在将数据源中的数据分配给视图模型和一个单独的变量。然后尝试返回people_list,而不是mymodelpeople_list的类型为List<Person>,而mymodel的类型为List<PersonViewModel>。 最简单的方法就是通过使用viewmodel摆脱这些变量并简化它。