簡體   English   中英

我們如何使用LINQ從asp.net mvc3中的長字符串中提取短字符串?

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

我目前正在從事一個小型電子商務項目。 我想顯示一個產品的小描述,而不是顯示所有描述(來自數據庫)。
這是我的控制器;

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

這是我的視圖代碼;

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

現在在我的部分“ _ProductPartial”視圖中,我將其描述字段稱為;

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

現在,我要顯示產品的簡短描述(而不是顯示默認情況下起作用的整個描述)。
那么,如何使用LINQ做到這一點?
還有其他方法(如果可能)嗎?

我正在通過使用ViewModel做到這一點:

    public string ProductDescription { get; set; }

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

對於您來說,如果您不想創建ViewModel,則可以擴展Product類以使其具有只讀屬性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM