1

I am struggling to default CId and SId to 1 if the parameters are empty,

  public ViewResult Index(int? CId,int?SId,string name,int? p)
    {

        if (CId == 0 || SId == 0)
        {
            CId = 1;
            SId = 1;
        }

Then I will use the values for a normal query. Thanks for any help

2 Answers 2

3

Cid and Sid is Nullable, so you can use HasValue property to check if the variable has value or not (null)

  public ViewResult Index(int? CId,int?SId,string name,int? p)
  {
        if (!CId.HasValue || !SId.HasValue)
        {
            CId = 1;
            SId = 1;
        }
  }
Sign up to request clarification or add additional context in comments.

Comments

0

Just curious but have you tried:

public ViewResult Index(string name,int? p,int? CId = 1,int? SId = 1)
    {


    }

You have to rearrange them, because the default valued parameters have to come last. Also, being that they're nullable, I'm not actually sure if this will work.

1 Comment

Thanks for the help but I can follow the logic in the other one and have tested it and it has worked. Did not test yours though

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.