读取视图数据时如何添加选择参数?

发布于 2024-12-10 05:35:55 字数 1340 浏览 0 评论 0 原文

我正在 asp.net 中制作一个 MVC 3 Web 应用程序。它应该像一个小社区,用户可以在其中互相发送消息(后来也可以发送给指定的人群)。因此,在我的数据库中,我有表“用户”和“消息”。消息(消息)中的一行包含发送者和接收者(用户表的外键)以及一些其他信息,例如标题、发送日期,当然还有消息文本。

当用户访问登录用户的索引页面时,我想显示已发送给该用户或从该用户发送的所有消息。因此,当从消息表中读取消息时,我想使用一个参数,即登录用户的 ID(我可以在控制器中访问该参数)。所以我有参数,但我不知道在获取结果时如何使用它。我知道必须有一种方法可以正确地执行此操作,而不是以丑陋的方式放入一堆 sql 并在代码中启动连接。

正如您现在可能看到的,表中的所有消息都将被检索并发送到视图以将其显示给用户。

CommunityEntities 行是整个实体框架事物的一部分,虽然我一直在遵循一些教程,但还不完全确定这些东西是如何工作的:

(创建 an-entity-framework-data-model-for-an-asp-net-mvc-application)

(Mvc-music-store-part-1)。

UserController.cs:

...

CommunityEntities db = new CommunityEntities();

public ActionResult Index()
    {
        var messages = db.Messages.ToList();
        return View(messages);
    }

...

CommunityEntities.cs:

public class CommunityEntities : DbContext
{
    public DbSet<User> Users { get; set; }        
    public DbSet<Message> Messages { get; set; }
}

编辑:我确实用 db.Database.SqlQuery(...) 尝试过那件事,但这似乎是错误的,因为重点是正确且“整洁”地做到这一点。这是为了我的教育,我的老师会对应用程序的结构很挑剔。

I am making an MVC 3 web application in asp.net. It is supposed to be like a little community where users can send messages to each other (and later on also to specified groups of people). So in my database I have the tables Users and Messages. A row in Messages (a message) contains both sender and receiver (a foreign key to the Users table) plus some other info such as title, send date and of course the message text.

When a user come to the index page for logged in users, I want to show all messages that have been sent to this user, or sent from this user. Therefore, when reading the messages from the Messages table I want to use a parameter, the id of the logged in user (which I can access in my controller). So I have the parameter but I don't know how to use it when getting the results. I know there must be a way to do this correctly instead of putting in a bunch of sql and starting connections right in the code the ugly way.

As you maybe can see now, all the messages from the table will be retrieved and sent to the View for displaying it to the user.

The CommunityEntities line is part of the whole Entity Framework thing, not exactly sure how that stuff works yet, although I have been following some tutorials:

(Creating-an-entity-framework-data-model-for-an-asp-net-mvc-application)

(Mvc-music-store-part-1).

UserController.cs:

...

CommunityEntities db = new CommunityEntities();

public ActionResult Index()
    {
        var messages = db.Messages.ToList();
        return View(messages);
    }

...

CommunityEntities.cs:

public class CommunityEntities : DbContext
{
    public DbSet<User> Users { get; set; }        
    public DbSet<Message> Messages { get; set; }
}

EDIT: I did try that thing with db.Database.SqlQuery(...) but that seemed wrong, seeing as the point is to do this correctly and "neat". This is for my education and my teachers are going to be picky at the structure of the application.

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

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

发布评论

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

评论(1

野却迷人 2024-12-17 05:35:55

您需要一个 where 子句:

var messages = db.Messages.Where( m => m.SentById == id || m.SentToId == id ).ToList();

You need a where clause:

var messages = db.Messages.Where( m => m.SentById == id || m.SentToId == id ).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文