gpt4 book ai didi

c# - MVC Controller 和存储库模型

转载 作者:太空宇宙 更新时间:2023-11-03 18:42:05 25 4
gpt4 key购买 nike

我一直收到这样的错误:

传入字典的模型项的类型为“HomebaseSystemNew.Controllers.ProductRepository”,但此字典需要类型为“HomebaseSystemNew.Models.Product”的模型项

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"           Inherits="System.Web.Mvc.ViewPage<HomebaseSystemNew.Models.Product>" %>

我的仓库:

  using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HomebaseSystemNew.Models;

namespace HomebaseSystemNew.Controllers
{
public class ProductRepository
{

public Product GetProductID(int Pid)
{
DatabaseLinkedDataContext db = new DatabaseLinkedDataContext();

return db.Products.FirstOrDefault(ans => ans.Pid == Pid);
}
}

我的 Controller :

using System.Linq;

using System.Web.Mvc;

using HomebaseSystemNew.Models;


namespace HomebaseSystemNew.Controllers
{

public class ProductsController : Controller
{

public ActionResult EditProduct(int id)
{
ProductRepository repo = new ProductRepository();

repo.GetProductID(id);

return View(repo);
}
}

最佳答案

您返回的是 View 中的存储库,而不是产品。将其更改为:

var product = repo.GetProductID(id);

return View(product );

关于c# - MVC Controller 和存储库模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7794696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com