Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8993983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:19:06+00:00 2026-06-15T23:19:06+00:00

I have a piece of MVC 2.0 html code is like below, I want

  • 0

I have a piece of MVC 2.0 html code is like below, I want replace it with MVC 3.0 Razor code, but I can not make it work using one line code like MVC 2.0.

<legend>&nbsp;Please add review for product :<%: Model.Product.ProductName.Length > 25 ? (string)Model.Product.ProductName.Substring(0, 25) + "..." : (string)Model.Product.ProductName %>&nbsp;<i style="color:Red">(&nbsp;<%: (string)Model.Product.Category.CategoryName %>&nbsp;)</i></legend>

The only way I can make it work is like below, but it looks not neat. I don’t like it, anyone can give me one line code which is worked in MVC 3.0 Razor will be great appreciate!!!

<legend>&nbsp;Please add review for product :
    <b>
        @{
            if(Model.Review.Product.ProductName.Length > 25)
            {
                @Model.Review.Product.ProductName.Substring(0, 25); @:...
            }
            else
            {
                @Model.Review.Product.ProductName;
            }
         }
    </b>
    &nbsp;<i style="color:Red">(&nbsp;@Model.Review.Product.Category.CategoryName&nbsp;)</i>&nbsp;</legend>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:19:08+00:00Added an answer on June 15, 2026 at 11:19 pm

    The ternary operator (x > y ? x : y) is usable in Razor. You just have to wrap it in brackets, so you can clean it up a lot by doing this (I’ve created a variable for product name for clarity’s sake):

    @{ var productName = Model.Review.Product.ProductName; }
    @(productName.Length > 25 ? productName.Substring(0, 25) + "..." : productName)
    

    However, since what you’re doing is a fairly common task, you might want to think about creating an HTML helper method for the trimming:

    public static class HtmlHelpers
    {
        public IHtmlString Truncate(this HtmlHelper htmlHelper, string value, int maxLength)
        {
            if (htmlHelper == null)
                throw new ArgumentNullException("htmlHelper");
    
            return value != null && value.Length > maxLength
                ? new HtmlString(htmlHelper.Encode(value.Substring(0, maxLength)) + "&hellip;")
                : new HtmlString(htmlHelper.Encode(value));        
        }
    }
    

    Then you can just use: @Html.Truncate(Model.Review.Product.ProductName, 25) in your view, which is much cleaner.

    You could also define an extension method on string:

    public static class StringExtensions
    {
        public void Truncate(this string value, int maxLength)
        {       
            return value != null && value.Length > maxLength
                ? value.Substring(0, maxLength) + "..."
                : value;
        }
    }
    

    which would allow you to just do this: @Model.Review.Product.ProductName.Truncate(25).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a piece of code (below) that can get the text of an
Here I have piece of code, showing example of how I want to update
I have a piece of code like this: <div id=container runat=server> <div id=parent runat=server>
I have this piece of code in a function. I want to print the
I have an MVC app with a Partial View (below). When I add Html.EnableClientValidation(),
I have the following piece of code in my Spring MVC application: @RequestMapping(value =
I have a piece of code that was written by someone else before the
I have this piece of code: public static DateDialogFragment newInstance(Context context, DateDialogFragmentListener listener) {
I have a piece of Python code that is supposed to open a new
I have a piece of code: var tblGroupedMultiPassive = dtCSV.AsEnumerable() .Where(r => r.Field<String>(course_type_id) ==

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.