I have an Index view where I would like show a list of news article, the Text property is a string which contains a html
string coming from a html
editor; now the html
content could be really long, so I would like show only the first <p>
element. I am doing that:
public ActionResult Index()
{
var articles = db.Articles.ToList().Select(a => new{Title = a.Title,
Tags = a.Tags,
Id = a.Id,
Text = (System.Xml.Linq.XDocument.Parse(a.Text).Descendants("p").FirstOrDefault())
}).ToList();
return View(articles);
}
But in the html
string there is not a root node, so the Linq
query fall in exception, How I can manage this case?
Thanks in advance for any suggestion
No comments:
Post a Comment