C# .Net MVC 非静态字段、方法或属性需要对象引用

发布于 2024-11-03 17:15:40 字数 1478 浏览 0 评论 0原文

我是 C# 的大三学生,我无法使用搜索找到解决方案

我有一个数据库模型(EDM)

我在模型文件夹中创建了一个类文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace photostorage.Models
{
    public class PhotosRepository
    {
        private fotostorageEntities db = new fotostorageEntities();

        public IEnumerable<photos> FindUserPhotos(string userid)
        {
            return from m in db.photos
                   select m;
        }

        public photos GetPhotosById(int photoid)
        {
            return db.photos.SingleOrDefault(d => d.id == photoid);
        }
    }
}

下一个为该模型创建了一个控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using photostorage.Models;

namespace photostorage.Controllers
{
    public class PhotosController : Controller
    {
        //
        // GET: /Photos/
        public ActionResult ViewPhoto(string userid, int photoid)
        {
            photos CurrentPhoto = PhotosRepository.GetPhotosById(photoid);
            if (CurrentPhoto == null)
                return View("NotFound");
            else
                return View("ViewPhoto", CurrentPhoto);
        }
    }
}

在结果中我有一个错误:非静态字段、方法或属性 photostorage.Models.PhotosRepository.GetPhotosById(int) 需要对象引用;

数据库中的表名 - 照片 EDM 连接字符串名称 - fotostorageEntities

需要帮助,因为我真的不知道解决方案。

I'm a junior in C# and I cant find the solution using search

I have a database model (EDM)

I have a created a class file in models folder:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace photostorage.Models
{
    public class PhotosRepository
    {
        private fotostorageEntities db = new fotostorageEntities();

        public IEnumerable<photos> FindUserPhotos(string userid)
        {
            return from m in db.photos
                   select m;
        }

        public photos GetPhotosById(int photoid)
        {
            return db.photos.SingleOrDefault(d => d.id == photoid);
        }
    }
}

Next one a created a controller to this model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using photostorage.Models;

namespace photostorage.Controllers
{
    public class PhotosController : Controller
    {
        //
        // GET: /Photos/
        public ActionResult ViewPhoto(string userid, int photoid)
        {
            photos CurrentPhoto = PhotosRepository.GetPhotosById(photoid);
            if (CurrentPhoto == null)
                return View("NotFound");
            else
                return View("ViewPhoto", CurrentPhoto);
        }
    }
}

In results i have an error: An object reference is required for the nonstatic field, method, or property photostorage.Models.PhotosRepository.GetPhotosById(int);

Table name in database - photos
EDM connectionStrings name - fotostorageEntities

Need help cause I realy dont know the solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

败给现实 2024-11-10 17:15:40

您当前正在将 GetPhotosById 作为静态方法调用。您需要创建 PhotosRepository 的实例。

    public ActionResult ViewPhoto(string userid, int photoid)
    {
        PhotosRepository photosRepository = new PhotosRepository();
        photos CurrentPhoto = photosRepository.GetPhotosById(photoid);
        if (CurrentPhoto == null)
            return View("NotFound");
        else
            return View("ViewPhoto", CurrentPhoto);
    }

You are currently calling GetPhotosById as a static method. You'll need to create the instance of the PhotosRepository.

    public ActionResult ViewPhoto(string userid, int photoid)
    {
        PhotosRepository photosRepository = new PhotosRepository();
        photos CurrentPhoto = photosRepository.GetPhotosById(photoid);
        if (CurrentPhoto == null)
            return View("NotFound");
        else
            return View("ViewPhoto", CurrentPhoto);
    }
看透却不说透 2024-11-10 17:15:40
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using photostorage.Models;

namespace photostorage.Controllers
{
    public class PhotosController : Controller
    {
        PhotosRepository objPhotosRepository = new PhotosRepository();
        //
        // GET: /Photos/
        public ActionResult ViewPhoto(string userid, int photoid)
        {
            photos CurrentPhoto = objPhotosRepository.GetPhotosById(photoid);
            if (CurrentPhoto == null)
                return View("NotFound");
            else
                return View("ViewPhoto", CurrentPhoto);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using photostorage.Models;

namespace photostorage.Controllers
{
    public class PhotosController : Controller
    {
        PhotosRepository objPhotosRepository = new PhotosRepository();
        //
        // GET: /Photos/
        public ActionResult ViewPhoto(string userid, int photoid)
        {
            photos CurrentPhoto = objPhotosRepository.GetPhotosById(photoid);
            if (CurrentPhoto == null)
                return View("NotFound");
            else
                return View("ViewPhoto", CurrentPhoto);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文