在视图中访问 localdb 数据

Access localdb data in a view

我现在正在启动一个 ASP.NET 核心 mvc 项目,但在特定视图中显示信息时遇到问题。

我有 2 个模型,它们包含它们的特殊性,我需要在 HomeController 中声明的视图中访问这两个模型。这是其中一个模型:

public class Tipo_Logradouro
    {
        public int Id  { get; set; }
        public string Abreviatura { get; set; }
        public string Nome_Tipo { get; set; }
    }

我的目的是通过 cshtml table 显示“Abreviatura”和“Nome_Tipo”,所有数据都已注册。但是当我尝试时它给出了一个实例错误并且我无法呈现它。该模型的控制器方法是通过脚手架制作的。

我不知道是否可以这样做,或者它是否非常简单,但我正在开始,我想解决这个问题。

PS: 我试图展示信息的 cshtml 超出了它的范围。

您需要遍历 Tipo_Logradouro 个对象的列表,并在循环中读取 Abreviatura 和 Nome_Tipo 属性的值。

调试时是否从数据库中获取数据?或者您正在使用 in-memory 数据库?

I have 2 models that contain their specificity and I need to have access to both in a view declared in the HomeController.

虽然视图只能接受一种类型的模型,但您可以有多种方法在同一视图中显示多个不同的模型数据。

第一种方式:

可以将两个需要展示数据的model合并成一个新的model,然后在view中接收新的model来展示不同model的数据

public class Tipo_Logradouro
    {
        public int Id  { get; set; }
        public string Abreviatura { get; set; }
        public string Nome_Tipo { get; set; }
    }

另一个模型:

   public class Person
    {
        public int Id { get; set; }
        public string Name{ get; set; } 
    }

组合新模型:

public class CombineModel
    {
        public List<Tipo_Logradouro> Tipo_Logradouros { get; set; }
        public List<Person> Persons { get; set; }
    }

控制器:

public IActionResult ShowData()
        { 
            CombineModel combineModel = new CombineModel()
            {
                Persons = _context.Person.ToList(),
                Tipo_Logradouros = _context.Tipo_Logradouro.ToList()
            };
            return View(combineModel);
        }

查看:

@model CombineModel
@{
    ViewData["Title"] = "ShowData";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>ShowData</h1>

<table class="table table-bordered">
    <tr>
        <td>Id</td>
        <td>Name</td>
    </tr>
    @foreach (var item in (IEnumerable<Person>)Model.Persons)
    {
        <tr>
            <td>@item.Id</td>
            <td>@item.name</td>
        </tr>
    }
</table>
<br />
<br />
<table class="table table-bordered">
    <tr>
        <td>Id</td>
        <td>Abreviatura</td>
        <td>Nome_Tipo</td>
    </tr>
    @foreach (var item in (IEnumerable<Tipo_Logradouro>)Model.Tipo_Logradouros)
    {
        <tr>
            <td>@item.Id</td>
            <td>@item.Abreviatura</td>
            <td>@item.Nome_Tipo</td>
        </tr>
    }
</table>

第二种方式:

您可以使用 ViewData 传递另一个模型来查看 :

控制器:

 public IActionResult ShowData()
        {
            List<Tipo_Logradouro> data = _context.Tipo_Logradouro.ToList();
            ViewData["data"] = data;
            List<Person> persons= _context.Person.ToList();
            return View(persons);
        }

查看:

@model IEnumerable<Person>
@{
    ViewData["Title"] = "ShowData";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>ShowData</h1>

<table class="table table-bordered">
    <tr>
        <td>Id</td>
        <td>Name</td>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>@item.Id</td>
            <td>@item.name</td>
        </tr>
    }
</table>
<br /><br />
<table  class="table table-bordered">
    <tr>
        <td>Id</td>
        <td>Abreviatura</td>
        <td>Nome_Tipo</td>
    </tr>
    @foreach (var item in (IEnumerable<Tipo_Logradouro>)ViewData["data"])
    {
        <tr>
            <td>@item.Id</td>
            <td>@item.Abreviatura</td>
            <td>@item.Nome_Tipo</td>
        </tr>
    }
</table>

测试结果如下: