在查看详细页面中添加评论部分

发布于 2025-01-19 02:06:44 字数 2903 浏览 0 评论 0原文

我正在开发 Bookevent 应用程序,我想在 eventdetail 页面上添加评论部分,我已经在使用 EventViewModel 来显示事件详细信息,并且我还添加了评论部分,以便用户可以发布评论。 所以基本上,我的问题是如何在同一视图页面上使用评论模型。我搜索了一下,发现我们可以使用tuple。但我认为在这种情况下这不是一个好的做法。有人可以建议任何其他方式吗?

CommentViewModel.cs

 public class CommentViewModel
    {
        [Required]
        public string CommentAdded { get; set; }

        [Required]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime Date { get; set; }

        [Required]
        public int EventId { get; set; }
        
    }

EventDetail.cshtml

@model EventViewModel

<div class="container">
    <div class="row">
        <h2 class="mt-5 mb-5 text-center">Event Details</h2>
        <div class="table-responsive field ms-5 mt-3">
            <table class="table" id="stu_table">
                <tbody>
                    <tr>
                        <th>Title</th>
                        <td>Title</td>
                    </tr>
                    <tr>
                        <th>Date</th>
                        <td>Date</td>
                        s
                    </tr>
                    <tr>
                        <th>Location</th>
                        <td>Location</td>
                    </tr>
                    <tr>
                        <th>StartTime</th>
                        <td>StartTime</td>
                    </tr>
                    <tr>
                        <th>Type</th>
                        <td>Type</td>
                    </tr>
                    <tr>
                        <th>Duration</th>
                        <td>Duration</td>
                    </tr>
                    <tr>
                        <th>Description</th>
                        <td>Description</td>
                    </tr>
                    <tr>
                        <th>OtherDetails</th>
                        <td>OtherDetails</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    <div class="row">
        <form asp-controller="Comment" asp-action="CreateComment">
            <div>   
                <label asp-for="CommentAdded"class="control-label">Add Comment</label>
                <textarea asp-for="CommentAdded"rows="4" cols="50"></textarea>
                <input class="btn-btn-default" value="Create" type="submit"/>
            </div>
        </form>
    </div>
</div>

I am working on Bookevent application I want to add comment section on eventdetail page where I am already using EventViewModel to display information of event detail and I also what to add comment section so user can post the comment.
So basically, My question is how can I use comment model as well on same view page. I searched for it and I found that we can use tuple. But I think that's not good practice in this case. Can Someone suggest any other way please.

CommentViewModel.cs

 public class CommentViewModel
    {
        [Required]
        public string CommentAdded { get; set; }

        [Required]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime Date { get; set; }

        [Required]
        public int EventId { get; set; }
        
    }

EventDetail.cshtml

@model EventViewModel

<div class="container">
    <div class="row">
        <h2 class="mt-5 mb-5 text-center">Event Details</h2>
        <div class="table-responsive field ms-5 mt-3">
            <table class="table" id="stu_table">
                <tbody>
                    <tr>
                        <th>Title</th>
                        <td>Title</td>
                    </tr>
                    <tr>
                        <th>Date</th>
                        <td>Date</td>
                        s
                    </tr>
                    <tr>
                        <th>Location</th>
                        <td>Location</td>
                    </tr>
                    <tr>
                        <th>StartTime</th>
                        <td>StartTime</td>
                    </tr>
                    <tr>
                        <th>Type</th>
                        <td>Type</td>
                    </tr>
                    <tr>
                        <th>Duration</th>
                        <td>Duration</td>
                    </tr>
                    <tr>
                        <th>Description</th>
                        <td>Description</td>
                    </tr>
                    <tr>
                        <th>OtherDetails</th>
                        <td>OtherDetails</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    <div class="row">
        <form asp-controller="Comment" asp-action="CreateComment">
            <div>   
                <label asp-for="CommentAdded"class="control-label">Add Comment</label>
                <textarea asp-for="CommentAdded"rows="4" cols="50"></textarea>
                <input class="btn-btn-default" value="Create" type="submit"/>
            </div>
        </form>
    </div>
</div>

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

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

发布评论

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

评论(1

捂风挽笑 2025-01-26 02:06:44

“我想在EventDetail页面上添加评论部分,我已经在使用EventViewModel”,“

好的,您有2个选项。

  1. 使用 viewbag viewData [] 将其他数据从控制器传递到视图,然后显示它,您仍然可以使用EventViewModel,而无需更改其中的任何内容。

在这种情况下,尽管您不能使用“ asp-for =” 从ViewBag/viewData []中获得的东西。

如果要在提交表单后将数据发送回控制器,请使用“ name =” ,并在控制器操作中使用相同的字符串名称进行参数。

  1. 我更喜欢的方法是将特定视图中所需的一切都放在其自己的视图模型中。如果您已经有一个视图模型EventViewModel,则只需在其中添加commentViewModel(具有另一个视图模型的视图模型)。

您的代码看起来像这样:

        public class EventViewModel
        {
            public CommentViewModel CommentViewModel { get; set; }

            // the usual properties you have for EventViewModel
        }

"I want to add comment section on eventdetail page where I am already using EventViewModel"

Okay, so you have 2 options.

  1. Use ViewBag or ViewData[] to pass the additional data from the controller to the View and then display it, you can still use your EventViewModel as well without changing anything in it.

In this case though you can't use "asp-for=" for the things you got from the ViewBag/ViewData[].

If you want to send the data back to the controller after submitting the form, use "name=" and make a parameter in your controller action with the same string name.

  1. The method I prefer is to just put everything that you need for a particular view in its own view model. If you already have a view model EventViewModel, just add CommentViewModel inside of it (view model that has another view model).

Your code will look like this:

        public class EventViewModel
        {
            public CommentViewModel CommentViewModel { get; set; }

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