从 SQL 视图中 ASP.NET MVC 应用程序中检索数据

Retrieve data from SQL view inside ASP.NET MVC application

我已经阅读了与此问题相关的所有问题,我能找到的所有视频,但我无法在其中找到解决问题的方法。

我一直在尝试使用 ASP.NET MVC 框架在 .cshtml 页面中显示来自 SQL 视图的数据。

这是我控制器中的代码:

public ActionResult childinfo()
{
       var myQuery1 = db.WebChildCaretakerInfo.Where(s => s.CustSSN == Custssn).Select(s => new { FirstName = s.FirstName, age = s.age, program_name = s.program_name });

        return View(myQuery1);
} 

其中 WebChildCaretakerInfo 是我要从中获取数据的视图。

这是我的 cshtml 页面中的代码:

@model IEnumerable<WebProgProject.Models.WebChildCaretakerInfo>

@{Layout = null;}

@{ 
var childName = "";
var age = 0;
var caretakerName = "";
var caretakerEmail = "";
var caretakerPhone = "";
foreach (var item in Model)
{
    childName = @item.FirstName;
    age = (int)@item.age;
    caretakerName = @item.CaretakerName;
    caretakerEmail = @item.email;
    caretakerPhone = @item.phone_number;}
}

注意:我在后面的代码中使用 ChildName 和 read-only 文本框中的其他变量。

当我尝试执行此页面时,我在浏览器中收到此错误:

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery1[<>f__AnonymousType33[System.String,System.Nullable1[System.Int32],System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[WebProgProject.Models.WebChildCaretakerInfo]'.

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery1[<>f__AnonymousType33[System.String,System.Nullable1[System.Int32],System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[WebProgProject.Models.WebChildCaretakerInfo]'.

我尝试删除 header

@model IEnumerable<WebProgProject.Models.WebChildCaretakerInfo>

并执行,我得到一个不同的错误:

Object does not contain a definition for FirstName in childName = @item.FirstName

这当然是无稽之谈,因为它就在我的 WebChildCaretakerInfo.cs 文件中:

namespace WebProgProject.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    public partial class WebChildCaretakerInfo
    {
        public string FirstName { get; set; }
        public Nullable<int> age { get; set; }
        public string programCode { get; set; }
        public string CaretakerName { get; set; }
        public string email { get; set; }
        public string phone_number { get; set; }
        public string program_name { get; set; }
        public string SSN { get; set; }
        public string CustSSN { get; set; }
    }
}

从 SQL 视图将数据检索到 ASP.NET MVC 应用程序是否有不同于从 SQL table 检索数据的不同方法?因为在同一个应用程序中,我正在从 table 中检索数据,这没问题。

您需要特定类型:

@model IEnumerable<WebProgProject.Models.WebChildCaretakerInfo>

但是 select 正在处理匿名类型:

.Select(s => new { FirstName = s.FirstName, age = s.age, program_name = s.program_name })

基本上,匿名类型在这里不起作用。因为视图需要知道期望的类型。如果您需要自定义类型,那么您可以创建一个视图模型,但是根据名称听起来您想要的类型正是从您的数据库中查询的类型。所以只需删除 .Select() 子句:

var myQuery1 = db.WebChildCaretakerInfo.Where(s => s.CustSSN == Custssn);
return View(myQuery1);

这将 return 枚举 WebChildCaretakerInfo,这正是视图所期望的。

(请注意,如果您确实创建了一个视图模型,那么您可能无法直接从数据库中 select 它,因为视图模型的创建可能不是 LINQ to Entities 可以转换为的内容底层数据存储。在那种情况下,您需要引入一个中间步骤,它可能只是采用 .ToList() 的形式,它将结果具体化到内存中,然后您可以从内存中转换为您的视图模型类型。 )