- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我已经用谷歌搜索了,但仍然没有找到我要找的东西。我想要发生的是获取已发布的特定图像的所有评论。
@model IEnumerable<Project1.Models.Picture>
@{
Layout = "~/Views/Shared/_PictureLayout.cshtml";
ViewBag.Title = "Animal Pictures";
}
@foreach (var picture in Model)
{
long id = picture.PictureID;
<div class="picture">
@Html.ActionLink("Picture", "IndexPic", "Pictures", new { id = picture.PictureID
}, true)
<img src="" alt="@picture.File" />
Posted by @Html.ActionLink(picture.GetUsername(picture.UserID), "Index",
"Pictures", new { username = picture.GetUsername(picture.UserID) }, true)
at @picture.Posted
@picture.GetComments(picture.PictureID) **** HERE LIES THE PROBLEM!!!
</div>
}
返回的错误是Cannot implicitly convert type 'void' to 'object'
计划是获取图片 ID 并将其传递给一个方法,该方法将获取该图片的所有评论
public void GetComments(long pictureID)
{
DBContext db = new DBContext();
Picture picture = new Picture();
//PictureComment comments = new PictureComment();
var comments = from c in db.PictureComments
where pictureID == c.PictureID
orderby c.DateTime descending
select c;
foreach (var comment in comments)
{
Console.WriteLine(picture.GetUsername(comment.UserID));
Console.WriteLine(comment.Comment);
}
}
起初我认为这是与 foreach 循环有关,但是 picture.GetUsername() 方法工作正常。
是否有任何简单的解决方法,我说简单是因为我是 c# 的新手并且不了解所有概念/术语。谢谢。
最佳答案
你不应该使用 Console.WriteLine
并且你的方法应该返回一个 MvcHtmlString
。
当您在 cshtml 文件中的方法之前使用 @
符号时,这意味着该方法的结果将写入生成的 html 中。
这应该有效:
public MvcHtmlString GetComments(long pictureID)
{
DBContext db = new DBContext();
Picture picture = new Picture();
//PictureComment comments = new PictureComment();
var comments = from c in db.PictureComments
where pictureID == c.PictureID
orderby c.DateTime descending
select c;
StringBuilder sb = new StringBuilder();
foreach (var comment in comments)
{
sb.AppendLine(picture.GetUsername(comment.UserID));
sb.AppendLine(comment.Comment);
}
return new MvcHtmlString(sb.ToString());
}
这会解决你的问题,但我想你想在你的 html 中以某种方式格式化评论,所以最好的办法是返回一张图片的评论列表。
然后在您的 cshtml 文件中,使用 foreach
循环遍历它们,并使用所需的 html 正确格式化它们。
关于c# - 公共(public)无效方法不起作用。无法将类型 'void' 隐式转换为 'object',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21731796/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!