C# 如何在CSHTML网页ASP.NETMVC4上的项目下方添加注释框
我正在尝试创建一个用于写博客的网站。我有一个数据库连接到它,目前我只有博客帖子工作。我有一个视图,通过使用visualstudio完成的“List”类型显示所有博客文章。基本上,它会打印出我想要的所有数据库条目,这样其他用户就可以对这些帖子发表评论,所有评论都会出现在帖子的较低部分。我是网络开发新手,所以我不确定是否有更好的方法来实现这一点 这是我的CSHTML文件C# 如何在CSHTML网页ASP.NETMVC4上的项目下方添加注释框,c#,asp.net,html,asp.net-mvc-4,razor,C#,Asp.net,Html,Asp.net Mvc 4,Razor,我正在尝试创建一个用于写博客的网站。我有一个数据库连接到它,目前我只有博客帖子工作。我有一个视图,通过使用visualstudio完成的“List”类型显示所有博客文章。基本上,它会打印出我想要的所有数据库条目,这样其他用户就可以对这些帖子发表评论,所有评论都会出现在帖子的较低部分。我是网络开发新手,所以我不确定是否有更好的方法来实现这一点 这是我的CSHTML文件 @model IEnumerable<Blogger.Models.Article> @{ ViewBag.
@model IEnumerable<Blogger.Models.Article>
@{
ViewBag.Title = "ViewAllArticles";
}
<h2>View All Posted Blogs</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Author)
</th>
<th>
@Html.DisplayNameFor(model => model.Content)
</th>
<th>
@Html.DisplayNameFor(model => model.DatePosted)
</th>
<th>
@Html.DisplayNameFor(model => model.IsAcceptingComments)
</th>
<th>
@Html.DisplayNameFor(model => model.LastEdited)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Author)
</td>
<td>
@Html.DisplayFor(modelItem => item.Content)
</td>
<td>
@Html.DisplayFor(modelItem => item.DatePosted)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsAcceptingComments)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastEdited)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
我希望这些源代码可以帮助理解我试图解释的内容。我愿意接受所有建议。您可以将其作为局部视图进行分隔,并单独处理。最好不要把东西混在一起,这样可以使组件松散耦合 你还有两个选择。 1.对UI完全使用客户端机制,评论将由WebAPI后端处理。 2.第三方评论系统,如Discus
如果它对您有帮助,请将其标记为答案。我建议创建一个新模型以支持评论。然后,您可以将注释列表添加到帖子中,并在模型之间创建多对多映射。然后,您可以使用Web方法,允许添加链接到相关帖子的评论
明天我将尝试为您获取一些示例代码 好的,我看到了这个选择。我需要查看部分视图的定义,以了解如何将其应用到我的项目中。谢谢你的建议。好吧,我也会处理的。我想我真的很喜欢这个想法,因为我没有时间发布任何代码。你设法让它工作了吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Blogger.Models;
namespace Blogger.Controllers
{
public class ArticleController : Controller
{
private ArticleDBEntities1 _entities = new ArticleDBEntities1();
//
// GET: /Article/
public ActionResult Index()
{
return View(_entities.Articles.ToList());
}
public ActionResult Create()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude ="Id")]Article ArticleToCreate)
{
try
{
// Insert Logic Here
_entities.Articles.Add(ArticleToCreate);
_entities.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return RedirectToAction("Index");
}
}
//View All Posts from all Users
public ActionResult ViewAllArticles()
{
return View(_entities.Articles.ToList());
}
}
}