【问题标题】:How is my model and data dictionary instance incorrect?我的模型和数据字典实例如何不正确?
【发布时间】:2019-09-04 22:54:27
【问题描述】:

我正在使用 .net core 2.2 mvc 创建一个 Web 应用程序。我在将数据带回部分视图时遇到问题。似乎当我从我的 sql server 存储过程传递数据时,我收到了这个错误:处理请求时发生未处理的异常。 InvalidOperationException:传递到 ViewDataDictionary 的模型项的类型为“Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[DineEquip.Models.ReportSingle]”,但此 ViewDataDictionary 实例需要“DineEquip.Models.ReportSingle”类型的模型项. Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.EnsureCompatible(对象值)

我已经重写了模型、控制器和视图,甚至开始了一个新项目以查看是否是问题所在,但仍然出现相同的错误。我看到这里发布了这种类型的错误,但似乎没有一个答案能帮助我解决这个问题,因为我只是带回了一条记录。我希望有人能指出我做错了什么。

--------------------型号------------------------------------

using System.ComponentModel.DataAnnotations;

namespace DineEquip.Models
{
    public class ReportSingle
    {
        [Key]
        public int EquipmentID { get; set; }
        public string Location { get; set; }
        [Display(Name = "Name")]
        public string Name { get; set; }
        [Display(Name = "Serial Number")]
        public string SerialNo { get; set; }
        [Display(Name = "Make")]
        public string Make { get; set; }
        [Display(Name = "Model")]
        public string Model { get; set; }
        [Display(Name = "Fixed Asset Number")]
        public string FixedAssetNo { get; set; }


    }
}

--------上下文---------

namespace DineEquip.Data
{
    public class DineEquipContext : DbContext
    {
        public DbSet<Models.ReportSingle> ReportSerial { get; set; }   
    }
}

-------控制器-------

public IActionResult ReportSerial(string Serial)
        {

            var sqlSerial = new SqlParameter("@SearchSerialNo", Serial);

            return PartialView(_context.ReportSerial.FromSql("exec DiningEquipmentReportSerial @SearchSerialNo", sqlSerial));

        }

---视图--main----------

@model  ReportSingle
@{
    ViewData["Title"] = "SearchDetails";
}



<form asp-controller="DineEquip" asp-action="ReportSerial" method="post" role="form">
    <div class="form-group">
        <table>
            <tr>
                <td>
                    Serial Number to find :
                </td>
                <td>
                    <input type="text" id="Serial" asp-for="SerialNo" name="Serial" />
                </td>
                <td>
                    <input type="submit" value="Search" id="Search" />

                </td>
            </tr>
        </table>
    </div>
</form>

-----查看部分------

@model ReportSingle

<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.SerialNo)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.FixedAssetNo)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Make)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Model)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Location)
            </th>
        </tr>
    </thead>
    <tbody>

            <tr>
                <td>
                    @Html.DisplayFor(model => model.Name)
                </td>
                <td>
                    @Html.DisplayFor(model => model.SerialNo)
                </td>
                <td>
                    @Html.DisplayFor(model => model.FixedAssetNo)
                </td>
                <td>
                    @Html.DisplayFor(model => model.Make)
                </td>
                <td>
                    @Html.DisplayFor(model => model.Model)
                </td>
                <td>
                    @Html.DisplayFor(model => model.Location)
                </td>
            </tr>
    </tbody>
</table>

局部视图应该返回存储过程的结果。任何有关此问题的帮助/指导将不胜感激。

【问题讨论】:

  • 来自数据库的结果通常在内存查询中,因此是 IQueryable 并且在被引用之前不会执行。从_context.ReportSerial.FromSql(...) 返回的结果在内存中,技术上尚未执行以获得结果。在 .FromSql 的末尾尝试 .FirstOrDefault()。这应该执行查询并返回第一个结果。
  • 太棒了!非常感谢您的洞察力和及时的回应

标签: c# asp.net-core-mvc


【解决方案1】:

对于遇到此问题的其他人。 @Bret Lipscomb 是完全正确的。为了解决这个问题,我所要做的就是在我的上下文语句之后添加 .FirstOrDefault() ,如下所示:return PartialView("ReportSerial", _context.ReportSerial.FromSql("exec DiningEquipmentReportSerial @SearchSerialNo", sqlSerial).FirstOrDefault());

【讨论】:

    猜你喜欢
    • 2015-09-25
    • 2019-01-05
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 2013-11-09
    • 2017-09-18
    • 2011-06-04
    • 1970-01-01
    相关资源
    最近更新 更多