2

I have a Razor view Html.DisplayFor where I am displaying some text. Now I want to truncate the text using jQuery. I am using the jQuery code from this jQuery Truncate.

In my csHtml, I have something like follows, the BlogContent is some string that I am Binding. Of course I think I can do SubString etc in C# on the BlogContent to truncate. But I want to do it using jQuery. I have tried the below code but no luck.

@{ 
    Layout = null;
}

      <div class="blog-post-body">
    <p class="p-bottom-20">@Html.DisplayFor(modelItem => item.BlogContent)</p>
     </div>
    
    @section scripts
    {
    <script src="~/js/jquery.min.js"></script>
    
            <!-- Bootstrap -->
            <script src="~/bootstrap/js/bootstrap.min.js"></script>
    
     <script>
                $(document).ready(function () {
                    $('ul.pagination > li.disabled > a').addClass('page-link');
                    truncateText(".p-bottom-20", 100);
                });
    
                function truncateText(selector, maxLength) {
                    $(selector).text((i, txt) => txt.length > maxLength ? txt.substr(0, maxLength) + "..." : txt);
                };
    
            </script>
    
    }

I don't see any errors on the Browser Console related to this.

No console errors

6
  • 1
    Should be @section Scripts instead of @scripts. Commented Oct 12, 2022 at 17:44
  • 1
    Yea sure sorry copy and paste issue. Commented Oct 12, 2022 at 17:45
  • 1
    Don't you already have jQuery and Bootstrap scripts added in _Layout.cshtml? Why do you add them again inside your view? Commented Oct 12, 2022 at 17:52
  • 2
    Oh because on this view I am saying Layout=null. I don't want to use the Layout for this page. Please see the edited post. Commented Oct 12, 2022 at 17:55
  • 1
    Are you getting any errors on the browser console? Commented Oct 12, 2022 at 18:02

1 Answer 1

1

If you are using Layout = null; then @section Scripts doesn't work because the section is defined in _Layout.cshtml. You have to add a full html document in your view.

@{
    Layout = null;
}

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>title</title>
  </head>
  <body>
    <div class="blog-post-body">
      <p class="p-bottom-20">@Html.DisplayFor(modelItem => item.BlogContent)</p>
    </div>

    <script src="~/js/jquery.min.js"></script>
    <script src="~/bootstrap/js/bootstrap.min.js"></script>
    
    <script>
      $(document).ready(function() {
        $('ul.pagination > li.disabled > a').addClass('page-link');
        truncateText(".p-bottom-20", 100);
      });

      function truncateText(selector, maxLength) {
        $(selector).text((i, txt) => txt.length > maxLength ? txt.substr(0, maxLength) + "..." : txt);
      }
    </script>
  </body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much it works. I didn't know that. The other view I have also has Layout to null. I was trying to see whats wrong with it. This explains it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.