Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .Net Visual Studio默认控制器有错误_C#_Asp.net_Visual Studio 2013 - Fatal编程技术网

C# .Net Visual Studio默认控制器有错误

C# .Net Visual Studio默认控制器有错误,c#,asp.net,visual-studio-2013,C#,Asp.net,Visual Studio 2013,我为显示小图像(我不知道如何放大它们)和显示大量代码而提前道歉,但其中大多数只是供参考,并为读者简要介绍我正在做的事情 我对.Net和Visual Studio非常陌生。我正在尝试实现一个名为Inventory的数据库。它有一个多对多的关系!(产品类别)。目前我已经为它创建了页面(下图)。我对CRUD方法使用了VisualStudio的默认控制器,但现在它们有我无法修复的错误。我已经标记了CategoryController.cs中的所有错误 注:我可以上传项目,如果有人足够友好,看看它,因为它

我为显示小图像(我不知道如何放大它们)和显示大量代码而提前道歉,但其中大多数只是供参考,并为读者简要介绍我正在做的事情

我对.Net和Visual Studio非常陌生。我正在尝试实现一个名为Inventory的数据库。它有一个多对多的关系!(产品类别)。目前我已经为它创建了页面(下图)。我对CRUD方法使用了VisualStudio的默认控制器,但现在它们有我无法修复的错误。我已经标记了CategoryController.cs中的所有错误

注:我可以上传项目,如果有人足够友好,看看它,因为它更容易这样做

这是类别的索引页

当我点击饮料的详细信息时,它会列出所有具有该类别ID的产品。(饮料=1)

错误列表(右键单击->在新选项卡中打开图像)

型号/类别.cs

using System.Collections.Generic;

namespace Inventory.Models
{
    public class Category
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public List<Product> Products { get; set; } 
    }
}
使用System.Collections.Generic;
名称空间清单.Models
{
公共类类别
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共列表产品{get;set;}
}
}
控制器/类别控制器.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Inventory.DAL;
using Inventory.Models;

namespace Inventory.Controllers
{
    public class CategoryController : Controller
    {
        private InventoryContext db = new InventoryContext();

        // GET: Category
        public ActionResult Index()
        {
            return View(db.Categories.ToList());
        }

        // GET: Category/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Category category = db.Categories.Find(id);  <--Line 31 Error 3 and 4
            if (category == null)
            {
                return HttpNotFound();
            }
            return View(category);
        }

        // GET: Category/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Category/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,Name")] Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.Add(category);
                db.SaveChanges();      <-- Line 55 Error 5
                return RedirectToAction("Index");
            }

            return View(category);
        }

        // GET: Category/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Category category = db.Categories.Find(id);<--Line 69 Error 6 and 7
            if (category == null)
            {
                return HttpNotFound();
            }
            return View(category);
        }

        // POST: Category/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,Name")] Category category)
        {
            if (ModelState.IsValid)
            {
                db.Entry(category).State = EntityState.Modified; <--Line 86 Error 8
                db.SaveChanges(); <--Line 87 Error 9
                return RedirectToAction("Index");
            }
            return View(category);
        }

        // GET: Category/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Category category = db.Categories.Find(id); <--Line 100 Error 10 and 11
            if (category == null)
            {
                return HttpNotFound();
            }
            return View(category);
        }

        // POST: Category/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Category category = db.Categories.Find(id); <--Line 113 Error 12 and 13
            db.Categories.Remove(category);
            db.SaveChanges(); <--Line 115 Error 14 
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose(); <--Line 123 Error 15
            }
            base.Dispose(disposing);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统数据;
使用System.Data.Entity;
使用System.Linq;
Net系统;
使用System.Web;
使用System.Web.Mvc;
使用Inventory.DAL;
使用库存模型;
命名空间清单.控制器
{
公共类类别控制器:控制器
{
私有InventoryContext db=新的InventoryContext();
//获取:类别
公共行动结果索引()
{
返回视图(db.Categories.ToList());
}
//获取:类别/详细信息/5
公共行动结果详细信息(int?id)
{
if(id==null)
{
返回新的HttpStatusCodeResult(HttpStatusCode.BadRequest);
}

Category Category=db.Categories.Find(id);但是为了能够搜索id,它必须是一个intI,它说Inventory.Models.Category不包含IdCategory=db.Categories.Find(new Category(){id=id})的定义;.List InventoryContext.Categories!System.Collections.GenericList Find(System.Predicate)的最佳重载方法匹配有一些无效的参数运算符“==”不能应用于“int”和“inventory.Models.Category”类型的操作数我已经修复了它!感谢您的输入
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Inventory.DAL;
using Inventory.Models;

namespace Inventory.Controllers
{
    public class CategoryController : Controller
    {
        private InventoryContext db = new InventoryContext();

        // GET: Category
        public ActionResult Index()
        {
            return View(db.Categories.ToList());
        }

        // GET: Category/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Category category = db.Categories.Find(id);  <--Line 31 Error 3 and 4
            if (category == null)
            {
                return HttpNotFound();
            }
            return View(category);
        }

        // GET: Category/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Category/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,Name")] Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.Add(category);
                db.SaveChanges();      <-- Line 55 Error 5
                return RedirectToAction("Index");
            }

            return View(category);
        }

        // GET: Category/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Category category = db.Categories.Find(id);<--Line 69 Error 6 and 7
            if (category == null)
            {
                return HttpNotFound();
            }
            return View(category);
        }

        // POST: Category/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,Name")] Category category)
        {
            if (ModelState.IsValid)
            {
                db.Entry(category).State = EntityState.Modified; <--Line 86 Error 8
                db.SaveChanges(); <--Line 87 Error 9
                return RedirectToAction("Index");
            }
            return View(category);
        }

        // GET: Category/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Category category = db.Categories.Find(id); <--Line 100 Error 10 and 11
            if (category == null)
            {
                return HttpNotFound();
            }
            return View(category);
        }

        // POST: Category/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Category category = db.Categories.Find(id); <--Line 113 Error 12 and 13
            db.Categories.Remove(category);
            db.SaveChanges(); <--Line 115 Error 14 
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose(); <--Line 123 Error 15
            }
            base.Dispose(disposing);
        }
    }
}
using System.Collections.Generic;
using Inventory.Models;
using System.Linq;

namespace Inventory.DAL
{
    public class InventoryContext
    {
        public List<Product> Products { get; set; }
        public List<Category> Categories { get; set; }

        public InventoryContext()
        {
            Products = new List<Product>
            {
            new Product{Id=1,CategoryId=1,Brand="Coca Cola", Name="Coca Cola",Barcode="00001",Price=150},
            new Product{Id=2,CategoryId=1,Brand="Pepsi", Name="Pepsi",Barcode="00011",Price=150},
            new Product{Id=3,CategoryId=2,Brand="Homebrand", Name="Baked Beans",Barcode="0002",Price=250},
            new Product{Id=4,CategoryId=2,Brand="Homebrand", Name="Baked Patatos",Barcode="0022",Price=250}

            };
            Categories = new List<Category>
            {
            new Category{ID=1, Name="Drink", Products = Products.Where(p => p.CategoryId == 1).ToList() },
            new Category{ID=2,Name="Canned Food", Products = Products.Where(p => p.CategoryId == 2).ToList() }

            };
            foreach (var product in Products)
                product.Categories = Categories.Where(c => c.ID == product.CategoryId).ToList();

        }
    }
}