C# .Net MVC 非静态字段、方法或属性需要对象引用
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您当前正在将
GetPhotosById
作为静态方法调用。您需要创建PhotosRepository
的实例。You are currently calling
GetPhotosById
as a static method. You'll need to create the instance of thePhotosRepository
.