[RESOLVED]How to get the first paragraph (<p>…</p>)

I’m using asp.net webpages for my blog management system. I want to get only the first paragraph (<p>….. some text …</p>) from the content field in the database. Btw, I use
@Html.Raw(@Server.HtmlDecode(@p.Content).ToString()) to extract the content field.
Thanks,

I suppose you could probably use a Regular Expression to grab the contents of the first paragraph that appears :

@Html.Raw(@Server.HtmlDecode(@Regex.Matches(p.Content,"<p>(.*?)</p>").Cast<Match>().FirstOrDefault()).ToString())

You can see a very simplistic example here.

Rion,

Please find the
error message
I received by placing the code you provided. Any suggestion?

It’s better if you post the error message here in the forums because it seems to have gone from your site. You should also show how you attempted to apply Rion’s suggestion.

Incidentally, you do not need to prefix all the variables and method calls with @ signs. You only need to prefix it to the first one in the chain:

@Html.Raw(p.Content)

You also do not need to use Server.HtmlDecode as Html.Raw does that for you, and there is no need to call ToString() on a string.

Have you included the appropriate using statement at the top of your Page?

@using System.Text.RegularExpressions;

As mentioned by Mike, the multiple ‘@’ characters can be left out (as the first one will begin your ‘code-block‘) :

@Html.Raw(Regex.Matches(p.Content,"<p>(.*?)</p>").Cast<Match>().FirstOrDefault())

Is p.Content a string? Are you sure that it isn’t null? Any additional information that you could provide about how this is implemented would be extremely helpful.

Rion,

Yes, I put the using expression at the top of the file. I do also remove the multiple ‘@’ characters as per Mike’s suggestion. I do also change your previous code with the new one but still I’m getting error as displayed in http://razor.nacds.org/Test

Any idea?

That means that p.Content is null. Without seeing any of your code, it might be that p is null or just p.Content. You need to test for null:

@if(p.Content != null){
   @Html.Raw(Regex.Matches(p.Content).Cast<Match>().FirstOrDefault())
}

If that errors, try this:

@if(p != null){
    @Html.Raw(Regex.Matches(p.Content).Cast<Match>().FirstOrDefault())
}

Mike,

I don’t know what I missed but still the code is not working.

Nebi

Mike,

I don’t know what I missed but still the code is not working.

We are all in to the dark too. That’s because you still haven’t shown your code.

Mike,
Please find below the code I use to access the data

@using System.Text.RegularExpressions; @{ Layout = "~/_Layout.cshtml"; Page.Title = "Home"; var pageSize = 15; var totalPages = 0; var count = 0; var page = UrlData[0].IsInt() ? UrlData[0].AsInt() : 1; var offset = (page - 1) * pageSize; var db = Database.Open("nacds1ConnectionString1"); var sql = "SELECT Count(*) FROM Post_New INNER JOIN Category_New ON Post_New.CategoryID = Category_New.CategoryID"; count = (int)db.QueryValue(sql); totalPages = count / pageSize; if (count % pageSize > 0) { totalPages += 1; } sql = "Select Post_New.ID, PostID, Title, SubTitle, Photo, Video, Description, Slug, Content, CreatedOn, Name From Post_New " + "Inner Join Category_New on Post_New.CategoryID = Category_New.CategoryID " + "ORDER BY CreatedOn DESC OFFSET @0 ROWS FETCH NEXT @1 ROWS ONLY;"; var result = db.Query(sql, offset, pageSize); } @foreach (var p in result) { if (p.PostID != null) { <div class="isotope-post span4 @p.Name"> <div> <!-- Article Heading --> <div class="entry drop-shadow curved"> <h5 class="heading"> <a href="/Home/@p.PostID/@p.CreatedOn.ToString("yyyy-MM-dd")/@p.Slug"> @p.Title </a> </h5> <!-- Content --> @if(p.Content != null){ @Html.Raw(Regex.Matches(p.Content).Cast<Match>().FirstOrDefault()) } @*<p>@Html.Raw(@Server.HtmlDecode(@p.Content).ToString())</p>*@ <p><a href="/Home/@p.PostID/@p.CreatedOn.ToString("yyyy-MM-dd")/@p.Slug">Read More...</a></p> <!-- Meta --> <ul class="entry-meta"> <li class="time">@p.CreatedOn.ToString("MM/dd/yyyy") <span style="float:right;">@p.Name</span></li> </ul> </div> </div> </div> } else { <div class="isotope-post span4 @p.Name"> <div> <!-- Featured --> <div class="featured-stamp"><p>@p.Name</p></div> <!-- Thumbnail --> <a href="/Article/@p.ID/@p.CreatedOn.ToString("yyyy-MM-dd")/@p.Slug" class="fancybox entry-image"> <span class="entry-image-overlay"></span> <img src="~/Assets/thumbnails/pic5.jpg" alt="Featured" /> </a> <div class="entry drop-shadow"> <!-- Title --> <h5 class="heading"><a href="/Article/@p.ID/@p.CreatedOn.ToString("yyyy-MM-dd")/@p.Slug">@p.Title </a></h5> <!-- Content --> @*<p>@p.Description</p>*@ <!-- Meta --> <ul class="entry-meta"> <li class="time">@p.CreatedOn.ToString("MM/dd/yyyy") <span style="float:right;">@p.Name</span></li> </ul> </div> </div> </div> } } @section Pagination{ <ul class="pager"> @{ if(page > 1){ <li style="float:left;"><a href="/Default/@(page-1)">&larr; Previous</a></li> } for (var i = 1; i < totalPages + 1; i++){ <li style="display:none;"><a href="/Default/@i">@i</a></li> } if(page < totalPages){ <li style="float:right;"><a href="/Default/@(page + 1)">Next &rarr;</a></li> } } <p class="pagetotal"> Page @page of @totalPages </p> </ul> }

It looks like you are checking that your PostID is null, but just as a precaution, it probably wouldn’t explicitly hurt to check that p isn’t null as well in the beginning of your foreach loop :

if(p != null && p.PostID != null)
{
     // Code omitted for brevity
}

Besides that, the null check should be working (as I figured the issue was a result of the p.Content property being null). I suppose that need to include the actual Regular Expression as well (which apparently I had left out of my previous response)
:

@if(p.Content != null)
{
     @Html.Raw(Regex.Matches(content,"<p>(.*?)</p>").Cast<Match>().FirstOrDefault())
}

I’ve edited my previous responses to include this omission as well.

Still, I’m getting error – ‘System.Text.RegularExpressions.MatchCollection’ does not contain a definition for ‘Cast’

Try including a LINQ reference as well :

@using System.Text.RegularExpressions;
@using System.Linq;

I put the LINQ reference but it didn’t work.

Nebi

I put the LINQ reference but it didn’t work.

Did you receive an error at all?

The error is: ‘System.Text.RegularExpressions.MatchCollection’ does not contain a definition for ‘Cast’

Thanks all,

I managed to solve it finally. I changed the code to this:

Match fullcontent = Regex.Match(@p.Content, "<p>.*?</p>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
@Html.Raw(fullcontent)

Leave a Reply