【发布时间】:2023-04-03 10:53:01
【问题描述】:
我的目标
使用 C# ASP.Net MVC 保存 html 并将其发布到博客文章中,使用最佳实践
我该怎么做?
以下是导致以下错误的来源:
错误 2 运算符 '.'不能应用于“lambda”类型的操作数 表达'
ArticleContent.CS(控制器)
// POST: Article/Articles/Create
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "canEdit")]
public ActionResult Create([Bind(Include = "PostId,Title,PublishDate,CreateDate,ModifyDate,Author,PageBody,Feature,Published,Excerpt,CreateDateUTC,PublishDateUTC,ModifyDateUTC,CanonicalUrl,UrlSlug,Category,Tag")] ArticleContent articles)
{
if (ModelState.IsValid)
{
db.Articles.Add(articles);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(articles);
}
Article.cs(模型)
[Display(Name = "Body of Article")]
public string PageBody { get; set; }
index.cshtml(视图)
@Html.Raw(HttpUtility.HtmlDecode((modelItem => modelItem.PageBody).ToString().Substring(0,250)))
index.cshtml(完整版视图)
<section class="bg-ocean row light text-center shelve">
@if (User.IsInRole("canCreate"))
{
<button class="btn btn-info btn-lg btn-block">
@Html.ActionLink("Create New", "Create")
</button>
}
@foreach (var item in Model)
{
<article class="headline">
<div class="col-xs-12 col-md-7 headline-image np nm">
<a href="@Url.Action("Details", "Details", new { id = item.PostId })" target="_blank">
<img src="@Html.DisplayFor(modelItem => item.Feature)" />
<span>
@Html.DisplayFor(modelItem => item.Categories)
</span>
</a>
</div>
<div class="col-xs-12 col-md-5 bg-light headline-content np nm">
<a href="@Url.Action("Details", "Details", new { id = item.PostId })" target="_blank">
<small class="dark">@Html.DisplayFor(modelItem => item.ModifyDate)</small>
<h2 class="media-heading dark">@Html.DisplayFor(modelItem => item.Title)</h2>
<hr />
<p>@Html.DisplayFor(modelItem => item.Excerpt)</p>
</a>
<div class="headline-footer">
<div class="btn-group btn-group-justified">
<span class="btn btn-default" role="button"><i class="fa fa-eye"></i> 172</span>
<span class="btn btn-default" role="button"><i class="fa fa-comment"></i><a href="@Url.Action("Details", "Details", new { id = item.PostId + "%23disqus_thread" })"></a></span>
<span class="btn btn-default highlight" role="button"><i class="fa fa-share"></i> 210</span>
</div>
</div>
</div>
@if (User.IsInRole("canEdit"))
{
@Html.ActionLink("E", "Edit", new { id = item.PostId })
}
@if (User.IsInRole("canDelete"))
{
@Html.ActionLink("D", "Delete", new { id = item.PostId })
}
</article>
}
</section>
问题作为资源
@Html.Raw(HttpUtility.HtmlDecode((modelItem => modelItem.PageBody).ToString().Substring(0,250)))
<section class="bg-ocean row light text-center shelve">
@if (User.IsInRole("canCreate"))
{
<button class="btn btn-info btn-lg btn-block">
@Html.ActionLink("Create New", "Create")
</button>
}
@foreach (var item in Model)
{
<article class="headline">
<div class="col-xs-12 col-md-7 headline-image np nm">
<a href="@Url.Action("Details", "Details", new { id = item.PostId })" target="_blank">
<img src="@Html.DisplayFor(modelItem => item.Feature)" />
<span>
@Html.DisplayFor(modelItem => item.Categories)
</span>
</a>
</div>
<div class="col-xs-12 col-md-5 bg-light headline-content np nm">
<a href="@Url.Action("Details", "Details", new { id = item.PostId })" target="_blank">
<small class="dark">@Html.DisplayFor(modelItem => item.ModifyDate)</small>
<h2 class="media-heading dark">@Html.DisplayFor(modelItem => item.Title)</h2>
<hr />
<p>@Html.DisplayFor(modelItem => item.Excerpt)</p>
</a>
<div class="headline-footer">
<div class="btn-group btn-group-justified">
<span class="btn btn-default" role="button"><i class="fa fa-eye"></i> 172</span>
<span class="btn btn-default" role="button"><i class="fa fa-comment"></i><a href="@Url.Action("Details", "Details", new { id = item.PostId + "%23disqus_thread" })"></a></span>
<span class="btn btn-default highlight" role="button"><i class="fa fa-share"></i> 210</span>
</div>
</div>
</div>
@if (User.IsInRole("canEdit"))
{
@Html.ActionLink("E", "Edit", new { id = item.PostId })
}
@if (User.IsInRole("canDelete"))
{
@Html.ActionLink("D", "Delete", new { id = item.PostId })
}
</article>
}
</section>
How to save html to database and retrieve is properly
未来说明
将 .Substring(0,250) 与 null 值一起使用会产生错误,因为字符串 0 - 250 不存在。
【问题讨论】:
-
什么代码触发了错误?我猜这是 HtmlDecode 的东西,但如果你的帖子是具体的,那就太好了。
-
index.cshtml(查看截图),内容为:@Html.Raw(HttpUtility.HtmlDecode((modelItem => modelItem.PageBody).ToString().Substring(0,250)))
-
不应该只是
modelItem.PageBody.ToString()而不是(modelItem => modelItem.PageBody).ToString()吗?
标签: c# asp.net asp.net-mvc razor