MVC存储库和返回Json的接口

发布于 2024-12-19 04:13:52 字数 1383 浏览 1 评论 0原文

我是 MVC 新手,无法让它工作。我基本上有一个 Users 类、一个 UserRepository 和一个 IUser 接口。

这是我的代码:

public class Users
{

public string UserName { get; set; }
public string Department { get; set; }
public string UserType { get; set; }


}

 public class UsersRepository : TimeAttendanceMVC.Models.IUsers
{

    public Users Return_UserName_Dept()
    {
        Users U = new Users();
        List<Users> LoggedInUser = new List<Users>();

        U.UserName = "TestUser";
        U.Department = "Finance";
        U.UserType = "Administrator";

        LoggedInUser.Add(U);

        //string json = JsonConvert.SerializeObject(LoggedInUser, Formatting.Indented);
        //return json;

        return Json(LoggedInUser.ToArray(), JsonRequestBehavior.AllowGet);

    }


}

namespace TimeAttendanceMVC.Models
{
  public  class IUsers
    {
      List<Users> Return_UserName_Dept();

    }
}

我遇到了一些错误。在我返回 Json 的 UsersRepository.cs 中,错误显示“名称 Json 在当前上下文中不存在”。 IUsers.cs 的错误是“Return_UserName_Dept() 必须声明一个主体,因为它没有标记为抽象...”。

有人可以帮我解决这个问题吗?我只是不知道这是如何工作的,我正在尝试通过开发这个应用程序来学习 MVC。它实际上是在这里找到的 FullCalendar 应用程序 - FullCalendar 链接。我正在尝试将其变成 MVC 应用程序。


编辑:

也许我需要这样做:

public JsonResult Return_UserName_Dept()

而不是 public Users Return_UserName_Dept()

I'm new at MVC and can't get this to work. I basically have a Users class, a UserRepository, and a IUser interface.

This is my code:

public class Users
{

public string UserName { get; set; }
public string Department { get; set; }
public string UserType { get; set; }


}

 public class UsersRepository : TimeAttendanceMVC.Models.IUsers
{

    public Users Return_UserName_Dept()
    {
        Users U = new Users();
        List<Users> LoggedInUser = new List<Users>();

        U.UserName = "TestUser";
        U.Department = "Finance";
        U.UserType = "Administrator";

        LoggedInUser.Add(U);

        //string json = JsonConvert.SerializeObject(LoggedInUser, Formatting.Indented);
        //return json;

        return Json(LoggedInUser.ToArray(), JsonRequestBehavior.AllowGet);

    }


}

namespace TimeAttendanceMVC.Models
{
  public  class IUsers
    {
      List<Users> Return_UserName_Dept();

    }
}

There are a few errors that I get. In UsersRepository.cs where i'm returning Json, the error says that "The name Json does not exist in the current context". The error from IUsers.cs is that "Return_UserName_Dept() must declare a body because it is not marked abstract...".

Can anybody please help me with this. I just don't know how this is supposed to work and i'm trying to learn MVC by working on this application. It's actually the FullCalendar application found here - link to FullCalendar. I'm trying to turn it into an MVC application.


EDIT:

Maybe I need to do this:

public JsonResult Return_UserName_Dept()

instead of public Users Return_UserName_Dept()

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

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

发布评论

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

评论(1

琴流音 2024-12-26 04:13:52

您应该在控制器上使用某种返回 json 操作 (jsonresult) 的方法执行此操作。存储库应该只返回您的数据,并且您需要执行的所有操作,无论是将数据转换为 json 还是任何其他逻辑,都应该在控制器或控制器调用的某个帮助器类上发生。

编辑:

为了拥有返回 JsonResult 的方法,您需要引用 System.Web.Mvc.ActionResult 并且由于存储库通常位于模型中,因此您不会有此引用。另一件事是您可能打破你的设计逻辑应该可以在您想要的控制器

编辑2:

下面的代码来自您可以看到的旧帖子 此处。请注意操作 PopulateDetails 如何从存储库获取用户对象,这就是存储库所做的全部。实际逻辑发生在该方法内,例如填充其余部分UserModel 类,然后返回 JsonResult:

public JsonResult PopulateDetails(UserModel model)
{
    UserResultModel userResultModel = new UserResultModel();
    if (String.IsNullOrEmpty(model.UserId))
    {
        userResultModel.Message = "UserId can not be blank";
        return Json(userResultModel);
    }

    User user = _userRepository.GetUser(model.UserId);

    if (user == null)
    {
        userResultModel.Message = String.Format("No UserId found for {0}", model.UserId);
        return Json(userResultModel);
    }

    userResultModel.LastName = user.LastName;
    userResultModel.FirstName = user.FirstName;
    userResultModel.Message = String.Empty; //success message is empty in this case

    return Json(userResultModel);
}

You should be doing this on your controller in some method which returns a json action (jsonresult). The repository should be returning your data only and all the operations you need to do, whether you're converting data to json or any other logic should happen at the controller or at some helper class which would be called by the controller..

Edit:

In order to have a method which returns a JsonResult, you need to have a reference to System.Web.Mvc.ActionResult and since the repository is usually at the model, you won't have this reference.. another thing is that you might be breaking your design the logic should be available at the controller for what you want

Edit 2:

The code below is from an old post you can see here. Note how the action PopulateDetails gets the user object from the repository and that's all the repository does.. the actual logic is happening inside this method, such as populate the rest of the UserModel class, and then it returns the JsonResult:

public JsonResult PopulateDetails(UserModel model)
{
    UserResultModel userResultModel = new UserResultModel();
    if (String.IsNullOrEmpty(model.UserId))
    {
        userResultModel.Message = "UserId can not be blank";
        return Json(userResultModel);
    }

    User user = _userRepository.GetUser(model.UserId);

    if (user == null)
    {
        userResultModel.Message = String.Format("No UserId found for {0}", model.UserId);
        return Json(userResultModel);
    }

    userResultModel.LastName = user.LastName;
    userResultModel.FirstName = user.FirstName;
    userResultModel.Message = String.Empty; //success message is empty in this case

    return Json(userResultModel);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文