0

I have created some static functions that return integer. In my view I want sum of these functions. I am using following code:

@myrepository.OverDraftCount() +
@myrepository.MortgageCount() +
@myrepository.InstallmentCount()+
@myrepository.RevolvingCount()+
@myrepository.OthersCount()

But it is returning 2 + 2+ 2 + 2 + 2 instead of 10 which it considers all these function's output as string. How can I change it ?

Thanks

1
  • I'm happy you marked my answer as the right one, but after seeing @Darin Dimitrov's answer I have to say that his approach is probably cleaner. You could also leave it halfway and perform the addition in your controller and pass the result to the view through the ViewBag property; like: ViewBag.Sum = // operation and in the view @ViewBag.Sum Commented Apr 3, 2011 at 9:24

2 Answers 2

6

You just forgot to add a Sum method to this class:

public decimal Sum()
{
    return OverDraftCount() + 
           MortgageCount() + 
           InstallmentCount() + 
           RevolvingCount() + 
           OthersCount();
}

so that in your view:

@myrepository.Sum()

Views should be as dumb as possible. They are so dumb that are not even capable of adding numbers. Do not code such logic into them. They should only show information that is passed to them, not try to calculate and fetch data. That's not their responsibility.

Sign up to request clarification or add additional context in comments.

Comments

-1

The Razor view engine will treat everything as a string (HTML rendering means outputting a bunch of strings), so you'll need to perform the sum in a code block and the display it.

Try:

@{ 
    var sum = myrepository.OverDraftCount() +
        myrepository.MortgageCount() +
        myrepository.InstallmentCount()+
        myrepository.RevolvingCount()+
        myrepository.OthersCount();
}

Then you can use the sum variable in your html:

<span>@sum</span>

EDIT to clarify all the upheaval:

As I have extensively stated in the comments, This will solve your problem, but it is hardly the neatest way to accomplish what you want. As I see it, you have 2 better options:

  1. Either take the approach in @Darin Dimitrov's answer or
  2. Perform the calculation in the controller and pass the sum value to the view, either as part of a View Model or through the ViewBag.

10 Comments

@DotnetSparrow - I'm happy you marked my answer as the right one, but after seeing @Darin Dimitrov's answer I have to say that his approach is probably cleaner. You could also leave it halfway and perform the addition in your controller and pass the result to the view through the ViewBag property; like: ViewBag.Sum = // operation and in the view @ViewBag.Sum.
It is not a good idea to do some calculations on view I think. It is not view's job. it is mostly the job of controller. you should do it on the controller and pass it with ViewBag or some strongly typed class. it would make it hard to unit-testing your app.
Dear downvoter, I respect the downvote, but could you at least explain why? My answer is not incorrect; I answer literally what the OP asks for. And, I have posted two comments encouraging the OP to go for Darin's answer. So why the downvote?
@Sergi there is no offense here but I am the down-voter :) and I explained it why it isn't correct way.
@tugberk - Fair enough. I still think my answer is not wrong, it's not the desired way to do (and I'd never do it myself) it but its answer's the OP question literally. I'm the first to say I do not deserve the right answer on this one, but wouldn't think I deserve a downvote either. Anyway, it's you opinion and I respect it. I'm happy you at least gave a reason for the downvote :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.