[英]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; 现在在我的部分“ _ProductPartial”视图中,我将其描述字段称为;
<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? 那么,如何使用LINQ做到这一点?
Is there any other way (if possible)? 还有其他方法(如果可能)吗?
I'm doing this by using ViewModel : 我正在通过使用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 对于您来说,如果您不想创建ViewModel,则可以扩展Product类以使其具有只读属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.