2
\$\begingroup\$

There are 28 check boxes that are given a common name and have different values:

<input type="checkbox" name="Schedules" id="checkbox1" value="1">

In controller in post action:

public ActionResult Create(SubscriptionPlanCreateViewModel subscriptionPlanCreateViewModel, FormCollection Form)
{
    string[] AllStrings = Form["Schedules"].Split(',');
    foreach (string item in AllStrings)
    {
        int value = int.Parse(item);
        if (value == 1)
        {
            subscriptionPlanCreateViewModel.Timeschedule = "OneTime";
        }
    }
}

By using this code I need to use 28 if conditions. How can I reduce the code size?

\$\endgroup\$
5
  • \$\begingroup\$ Are all the if conditions trying to set the Timeschedule property? Or are they all setting different properties on your view model? Also, do you have any control over how the view is generated, or are you restricted to just changing the controller code? \$\endgroup\$ Commented Nov 15, 2011 at 5:46
  • \$\begingroup\$ @StriplingWarrior No value==1 for time schedule for serp and passes value "onetime" to Timeschedule then value==2 passes value="Weekly" to timeschedule etc \$\endgroup\$ Commented Nov 15, 2011 at 5:50
  • \$\begingroup\$ So what if both checkbox 1 and 2 are selected? \$\endgroup\$ Commented Nov 15, 2011 at 5:54
  • \$\begingroup\$ if both the value 1 & 2 are selected the timeschedule is appended with value of 2 if (value == 1) { subscriptionPlanCreateViewModel.SerpRankingSchedule = "OneTime"; } if (value == 2) { subscriptionPlanCreateViewModel.SerpRankingSchedule = (subscriptionPlanCreateViewModel.SerpRankingSchedule == null || subscriptionPlanCreateViewModel.SerpRankingSchedule == "") ? "Weekly" : subscriptionPlanCreateViewModel.SerpRankingSchedule + " | Weekly"; } \$\endgroup\$ Commented Nov 15, 2011 at 5:57
  • \$\begingroup\$ the first 3 values are for SerpRankingSchedule then the next 3 values are for Reports schedule etc \$\endgroup\$ Commented Nov 15, 2011 at 5:59

2 Answers 2

2
\$\begingroup\$

You can use the checkboxes like this

<input type="checkbox" name="Timeschedule" id="checkbox1" value="OneTime">
<input type="checkbox" name="Timeschedule" id="checkbox2" value="Weekly">

and in the controller action the values from multiple checkboxes can be retrieved by using this method

string[] AllStrings = Form["Timeschedule"].Split(',');

or

string[] AllStrings = Request["Timeschedule[]"].Split(',');
\$\endgroup\$
3
\$\begingroup\$

It seems to me that what you really need to do is change the view code. For example, if you do this:

<input type="checkbox" name="Timeschedule" id="checkbox1" value="OneTime">
<input type="checkbox" name="Timeschedule" id="checkbox2" value="Weekly">

That will automatically bind to the Timeschedule property on your SubscriptionPlanCreateViewModel. However, if the user selects multiple checkboxes, the values will probably come across as comma-separated. You can either make Timeschedule an array (which would probably better represent what you're doing from a data perspective) or you can simply replace the commas with " | " to produce the string you say you want in your comments.

\$\endgroup\$
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.