How can we take a short string from a long string in asp.net mvc3 using LINQ?

I am currently working on a small ecommerce project. I want to show a small description for a product instead of displaying all the description (comming from a database). Here is my controller;

var products = context.Products.OrderBy(p => p.ProductName);
@ViewBag.ProductList = products.ToList<Product>();

And here is my view code;

<ul class="display" id="content">
@foreach( var item in @ViewBag.ProductList as IEnumerable<Product>)
{
    @Html.Partial("_ProductPartial", item)
}
</ul>

Now inside my Partial "_ProductPartial" view, i have that description field called;

<p>
    @Html.DisplayFor(model => model.ProductDescription)
</p>

Now, i want to display a short description for a product (instead of displaying the whole description which is by default works). So, How can i do this using LINQ? Is there any other way (if possible)?

Answers


I'm doing this by using ViewModel :

    public string ProductDescription { get; set; }

    public string ShortDescription
    {
        get
        {
            var text = ProductDescription;
            if (text.Length > 21)
            {
                text = text.Remove(19);
                text += "..";
            }
            return text ;
        }
    }

For you if you don't want to make a ViewModel you could extend your Product class to have this read only property


Need Your Help

Find 5 Largest Values In A Linked List in Processing/Java

java linked-list processing leap-motion

I got this code that gets x,y positions from a motion sensor tracking a hand. The app draws a circle in the middle of the screen and then detects whether the hand is outside the circle. While the ...