开发者

Add Values in model in View

I am very new to MVC 3 Razor in A开发者_JS百科Sp.net. i am sending a model to a view that is tightly coupled to this model. this model has list and i want to display a SUM of particular field in that model list. is that possible and how?


I wouldn't do it in the View.

I'd do it in the ViewModel:

public class SomeViewModel
{
   public ICollection<int> SomeValues { get; set; }
   public int MySum { get { return SomeValues.Sum(x => x.SomeValue); } }
}

Then the view:

@Html.DisplayFor(model => model.MySum)

Alternatively you could use a generic HTML helper, made generic so you can re-use across any model which has an IEnumerable<T> that can be aggregated.

Always try and keep your View clear of unnecessary logic.


This should do it, but without seeing some of your code it's hard to be exact:

@Model.YourList.Sum(p => p.PropertyNameYouWantToSum)


Decimal D = listItem.Where(P=>P.Amount>100).Sum(P=>P.Amount);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜