简体   繁体   English

System.Core.dll中发生类型'System.InvalidOperationException'的异常,但未在用户代码中处理

[英]Exception of type 'System.InvalidOperationException' occurred in System.Core.dll but was not handled in user code

I get the below excption: 我得到以下例外:

Exception of type 'System.InvalidOperationException' occurred in System.Core.dll but was not handled in user code System.Core.dll中发生类型'System.InvalidOperationException'的异常,但未在用户代码中处理

Additional information: The sequence does not contain Elements. 附加信息:序列不包含元素。

at line 在线

var kategoria = db.Kategorie.Include("Kursy").Where(k => k.NazwaKategorii.ToUpper() == nazwaKategori.ToUpper()).Single();

My KursyController.cs: 我的KursyController.cs:

using SklepInternetowy1.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace SklepInternetowy1.Controllers
{
public class KursyController : Controller
{

    private KursyContext db = new KursyContext();

    // GET: Kursy
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Lista(string nazwaKategori)
    {
        var kategoria = db.Kategorie.Include("Kursy").Where(k => k.NazwaKategorii.ToUpper() == nazwaKategori.ToUpper()).Single();
        var kursy = kategoria.Kursy.ToList();

        return View(kursy);
        //return View();
    }
    public ActionResult Szczegoly(string id)
    {
        var kurs = db.Kursy.ToList();
        return View(kurs);
        //return View();
    }

    [ChildActionOnly]
    public ActionResult KetagorieMenu()
    {
        var kategorie = db.Kategorie.ToList();
        return PartialView("_KategorieMenu", kategorie);
    }
}
}

My Lista.cs 我的Lista.cs

@{
ViewBag.Title = "Lista";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<div id="strona">

@Html.Action("KetagorieMenu")



<img src="../Content/Images/dommaly.png" width="5%" height="5%" />
<a href="link">
    Start > ASP.NET
</a>

@Html.Partial("_KursyList")

My _KategorieMenu.cshtml: 我的_KategorieMenu.cshtml:

@model IEnumerable<SklepInternetowy1.Models.Kategoria>
@using SklepInternetowy1.Infrastructure

<tabela>
<wiersz>

    @foreach (var kategoria in Model)
        {
        <komorka>
            <a href="@Url.Action("Lista","Kursy", new { nazwaKategori = kategoria.NazwaKategorii.ToString() })"
               style="background-image: url(@Url.IkonyKategoriiSciezka(kategoria.NazwaPlikuIkony)); ">
                @kategoria.NazwaKategorii
            </a>
        </komorka>
    }
</wiersz>

My _KursyList.cshtml: 我的_KursyList.cshtml:

@model IEnumerable<SklepInternetowy1.Models.Kurs>
@using SklepInternetowy1.Infrastructure


<div id="zawartosc">
<tabela>
    <wiersz>
        @foreach (var kurs in Model)
        {

            <komorka>
                <a class="dodajDoKoszykaLink" href="@Url.Action("DodajDoKoszyka","Koszyk",new {id = kurs.KursID})">
                </a>
                @if (kurs.Bestseller)
            {

            }

                <a class="kursSzczegolyLink" href="@Url.Action("Szczegoly","Kursy",new {id =kurs.KursID})">

                    <img class="kurs" src="@Url.ObrazkiSciezka(kurs.NazwaPlikuObrazka)" width="66%" height="66%" />
                    <h1>
                        @kurs.TytulKursu - @kurs.AutorKursu
                    </h1>
                </a>
            </komorka>
        }
    </wiersz>
</tabela>

How to solve this problem? 如何解决这个问题呢?

In this line: 在这一行:

var kategoria = db.Kategorie
                  .Include("Kursy")
                  .Where(k => k.NazwaKategorii.ToUpper() == nazwaKategori.ToUpper())
                  .Single();

You're calling .Single() , which means you're asserting that there is always exactly one record returned in the result. 您正在调用.Single() ,这意味着您断言结果中始终只返回一条记录。 The error is telling you that no records were returned in the result, so the call to .Single() fails. 该错误告诉您结果中返回任何记录 ,因此对.Single()的调用失败。

Since it's possible for no records to be returned in the result, instead use .SingleOrDefault() and check the result for null . 由于有可能在结果中不返回任何记录,因此请使用.SingleOrDefault()并检查结果是否为null Something like this: 像这样:

var kategoria = db.Kategorie
                  .Include("Kursy")
                  .Where(k => k.NazwaKategorii.ToUpper() == nazwaKategori.ToUpper())
                  .SingleOrDefault();
if (kategoria == null)
{
    // no record was found.
    // handle that situation in whatever way makes sense for your system.
}

If it's also possible that multiple records can be returned then you'd need to check for that as well, since both .Single() and .SingleOrDefault() will throw an error for multiple results (as their names imply, really). 如果它也有可能是条记录,可以再回到你需要以检查为好,因为这两种.Single().SingleOrDefault()将抛出多个结果错误(如他们的名字所暗示的,真的)。 Things like .FirstOrDefault() or additional .Where() clauses, etc. can be helpful to find the record you want in that case. .FirstOrDefault()或其他.Where()子句之类的内容在这种情况下有助于查找所需的记录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Core.dll中发生类型为&#39;System.invalidOperationException&#39;的异常,但未在用户代码中处理 - An exception of type 'System.invalidOperationException' occures in System.Core.dll but was not handled in user code System.Web.dll中发生类型&#39;System.InvalidOperationException&#39;的异常,但未在用户代码中处理 - An exception of type 'System.InvalidOperationException' occurred in System.Web.dll but was not handled in user code System.Web.Extensions.dll中发生类型为&#39;System.InvalidOperationException&#39;的异常,但未在用户代码中处理 - An exception of type 'System.InvalidOperationException' occurred in System.Web.Extensions.dll but was not handled in user code System.ServiceModel.dll中发生类型&#39;System.InvalidOperationException&#39;的异常,但未在用户代码中处理 - An exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.dll but was not handled in user code ExecuteReader- System.Data.dll中发生类型为“System.InvalidOperationException”的异常,但未在用户代码中处理 - ExecuteReader- An exception of type 'System.InvalidOperationException' occurred in System.Data.dll but was not handled in user code System.Drawing.dll 中出现类型为“System.InvalidOperationException”的异常,但未在用户代码中处理 - An exception of type 'System.InvalidOperationException' occurred in System.Drawing.dll but was not handled in user code EntityFramework.SqlServer.dll中发生类型为&#39;System.InvalidOperationException&#39;的异常,但未在用户代码中处理 - An exception of type 'System.InvalidOperationException' occurred in EntityFramework.SqlServer.dll but was not handled in user code EntityFramework.dll中发生类型&#39;System.InvalidOperationException&#39;的异常,但未在用户代码C#中处理 - An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code, C# EntityFramework.dll中出现“System.InvalidOperationException”类型的异常,但在注册时未在用户代码中处理 - An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code while Registering EntityFramework.dll中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理 - An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM