从数据库模型中抓取数据并在视图中显示它将不会显示

时间:2022-05-20 13:57:46

I am porting a windows desktop application to an ASP.NET MVC application. I have a bunch of tables in the database that I am able to create controllers with to generate views and it works fine. I want to create a page to display all the names that have a certain column that is set to false. Because there is no table that stores this data I have to manually query for the result using linq. So what I have done is ignored the creation of the model and created the controller.

我正在将一个Windows桌面应用程序移植到ASP.NET MVC应用程序。我在数据库中有一堆表,我可以创建控制器来生成视图,它工作正常。我想创建一个页面来显示具有设置为false的某个列的所有名称。因为没有存储此数据的表,所以我必须使用linq手动查询结果。所以我所做的就是忽略了模型的创建并创建了控制器。

    private raceEntities db = new raceEntities();

    // GET: Registrants
    public ActionResult Index()
    {
        var registrants = (from per in db.people
                          join personorgrole in db.personorganizationroles on per.PersonID equals personorgrole.PersonID
                          join personeve in db.personevents on personorgrole.PersonOrganizationRoleID equals personeve.PersonOrganizationRoleID
                          join eventcla in db.eventclasses on personeve.RaceEventID equals eventcla.RaceEventID
                          join racecla in db.raceclasses on eventcla.RaceClassID equals racecla.RaceClassID
                          join personeventcla in db.personeventclasses on eventcla.EventClassID equals personeventcla.EventClassID
                          where personorgrole.PersonID == 90946
                          select new { per.FirstName, per.LastName }).ToList();
        ViewBag.Res = registrants;

        return View();
    }

and in the view i have this code

在视图中我有这个代码

@model IEnumerable<IDFWebApp.Controllers.Custom.peopleController>
@{
    ViewBag.Title = "Index";
}

<ul>
    @foreach (var r in ViewBag.Res)
    {
        <li>
            @r
        </li>
    }
</ul>

Currently this query is for testing purposes to see if the data will show on the page, but it doesn't display the names.

目前,此查询用于测试目的,以查看数据是否将显示在页面上,但不显示名称。

2 个解决方案

#1


To expand on Karl's answer (anonymous types are internal and cant be accessed in the view), this article gives a good explanation of why it does not work

为了扩展Karl的答案(匿名类型是内部的,不能在视图中访问),本文给出了为什么它不起作用的一个很好的解释

You need to create a view model

您需要创建一个视图模型

public class PersonVM
{
  public string FirstName { get; set; }
  public string LastName { get; set; }
}

And modify the GET method to

并将GET方法修改为

public ActionResult Index()
{
    var registrants = (from per in db.people
                      join personorgrole in db.personorganizationroles on per.PersonID equals personorgrole.PersonID
                      ....
                      where personorgrole.PersonID == 90946
                      select new PersonVM
                      {
                          FirstName = per.FirstName, 
                          LastName = per.LastName
                      }).ToList();
    return View(registrants);
}

And modify the view to

并将视图修改为

@model IEnumerable<yourAssembly.PersonVM>
<ul>
    @foreach (var person in Model)
    {
        // Assuming you want to display the FirstName and LastName properties
        <li><span>@person.LastName</span><span>@person.LastName</span></li>
    }
</ul>

#2


Your LINQ projection is creating an anonymous type here:

你的LINQ投影在这里创建一个匿名类型:

select new { per.FirstName, per.LastName }

And while that is fine inside the controller, it is actually scoped as internal, which makes it inaccessible to the view. Thus your avoidance of using a server-side view model is making your ViewBag data unusable.

虽然在控制器内部没有问题,但它实际上是作为内部的范围,这使得视图无法访问。因此,避免使用服务器端视图模型会使ViewBag数据无法使用。

Make a server-side view model and pass it to the view. That is the standard approach to ASP.NET MVC development anyway.

创建服务器端视图模型并将其传递给视图。无论如何,这是ASP.NET MVC开发的标准方法。

Not saying the ViewBag is bad, but I would use it as a secondary approach instead of as a substitute to a view model.

不是说ViewBag很糟糕,但我会将它作为辅助方法而不是视图模型的替代品。

#1


To expand on Karl's answer (anonymous types are internal and cant be accessed in the view), this article gives a good explanation of why it does not work

为了扩展Karl的答案(匿名类型是内部的,不能在视图中访问),本文给出了为什么它不起作用的一个很好的解释

You need to create a view model

您需要创建一个视图模型

public class PersonVM
{
  public string FirstName { get; set; }
  public string LastName { get; set; }
}

And modify the GET method to

并将GET方法修改为

public ActionResult Index()
{
    var registrants = (from per in db.people
                      join personorgrole in db.personorganizationroles on per.PersonID equals personorgrole.PersonID
                      ....
                      where personorgrole.PersonID == 90946
                      select new PersonVM
                      {
                          FirstName = per.FirstName, 
                          LastName = per.LastName
                      }).ToList();
    return View(registrants);
}

And modify the view to

并将视图修改为

@model IEnumerable<yourAssembly.PersonVM>
<ul>
    @foreach (var person in Model)
    {
        // Assuming you want to display the FirstName and LastName properties
        <li><span>@person.LastName</span><span>@person.LastName</span></li>
    }
</ul>

#2


Your LINQ projection is creating an anonymous type here:

你的LINQ投影在这里创建一个匿名类型:

select new { per.FirstName, per.LastName }

And while that is fine inside the controller, it is actually scoped as internal, which makes it inaccessible to the view. Thus your avoidance of using a server-side view model is making your ViewBag data unusable.

虽然在控制器内部没有问题,但它实际上是作为内部的范围,这使得视图无法访问。因此,避免使用服务器端视图模型会使ViewBag数据无法使用。

Make a server-side view model and pass it to the view. That is the standard approach to ASP.NET MVC development anyway.

创建服务器端视图模型并将其传递给视图。无论如何,这是ASP.NET MVC开发的标准方法。

Not saying the ViewBag is bad, but I would use it as a secondary approach instead of as a substitute to a view model.

不是说ViewBag很糟糕,但我会将它作为辅助方法而不是视图模型的替代品。