1

I'm trying to get a random amount of paramaters in my controller. I think code will explain my issue :-)

In my view I've something like:

<%: Html.CheckBox("Time" + (i-1)) %>

where i is a random value (for example 4).

So, in case i was indeed 4, my controller method woul look like:

public ActionResult Test(int Time0, Time1, Time2, Time3) {
  //some code
}

But, the problem is I don't now i. So, the controller should accept a random amount of params.

Any ideas?

2 Answers 2

2

Create your checkboxes like this:

<%: Html.CheckBox("Time[" + i + "]") %>

Make sure i is sequential. That is, you cant have 1, 2, 3 and 5 (without 4). Actually you can have non-sequential indexes, but you would have to create another input for the index itself.

Your action should look like this:

public ActionResult SomeAction(int[] time)
{

}

Please refer to this link to read more about collection binding in ASP.NET MVC: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/

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

Comments

0

Make your argument like: int[] parameters.

2 Comments

This won't work. The C# params keyword does not apply for ASP.NET MVC, at least not using the default model binder.
@AndréPena you are right. +1 for correcting me and your answer.

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.