1

I have an MVC app where a user can select company benfits, there are 10 different benefits and they can have none up to all 10 of these. In my view I have the ten listed with radio buttons to indicate whether they are required or not. There is also a calculation performed in the controller that adds all the values together to give a total.

As an example in my controller -

newuser.LifeAssurance.LifeAssuranceRequired = viewModel.LifeAssuranceRequired;    
newuser.LifeAssurance.LifeAssuranceSchemeName = viewModel.LifeAssuranceSchemeName;
newuser.LifeAssurance.LifeAssuranceProviderName = viewModel.LifeAssuranceProviderName;
newuser.LifeAssurance.LifeAssuranceBenefitLevel = viewModel.LifeAssuranceBenefitLevel;
newuser.LifeAssurance.LifeAssuranceEmployerCost = viewModel.LifeAssuranceEmployerCost;
newuser.LifeAssurance.LifeAssuranceEmployeeCost = viewModel.LifeAssuranceEmployeeCost;

Since the user may decide not to choose this benefit is it possible to assign the cost as 0 if they have not made a selection in the view model? Can I check if it's null and add 0 in that case?

2 Answers 2

5

You can use the ?? operator (see here)
use it like this:

string someString = null;
string someOtherString = someString ?? "0";

if someString(or any other object) is not null use it, else use whatever is on the right of the ?? operator.

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

Comments

2

Maybe set your values as nullable by add ? to type, and then you can check it is null by:

var someValue = (nullableValue.HasValue) ? nullableValue.Value : 0;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.