I am using asp.net mvc 3 and thinking what the best way to handle this scenario is.
Say I have a form that creates a reward structure. In this case their can be many levels of rewards
example
$100 to $500 - get .05% back
$501 to $1000 - get 0.6% back
$1001 to $1001 - get 0.7% back
and so forth.
Now these tiers are entered in the form I am creating. There could be one reward tier or there could be 5 tiers or 50 tiers. I just don't know.
Option 1
Make an artificial limitation of say 5 tiers. If they only need one tier well they still have to go through the wizard form(through jquery) I am creating and skip the next 4 tier screens (there will be quite a few fields they will have to fill out so I decided to make it a wizard so it is not so overwhelming at once).
public class MasterVm
{
public IList<TiersVm> Tiers { get; set; }
public MasterVm()
{
Tiers = new List<TiersVm>();
}
}
@for (int i = 0; i < Model.Tiers.Count; i++)
{
@Html.LabelFor(x => x.Tiers[i].StartOfRange, "Start Of Range")
@Html.TextBoxFor(x => x.Tiers[i].StartOfRange)
@Html.LabelFor(x => x.Tiers[i].EndOfRange, "End Of Range")
@Html.TextBoxFor(x => x.Tiers[i].EndOfRange)
// more fields here
}
In my controller I would make 5 placeholders so the for loop would go 5 times around.
Option 2
Make the form have a generate another tier button. They click on it and another tier would be made. This way if they only have one they only see it once and if they have 100 it does not matter.
I was thinking of using jquery clone to achieve this but I don't know really how the view model binding works. Does it look at id's? or the name?
When I do option one all my controls look like this
<input id="Tiers_0__StartOfRange" type="text" value="0" name="Tiers[0].StartOfRange">
They all have unique id's(what is good) and unique names. I am unsure if in the clone code I should remove the id's and the names. Or do I need to have code in there to generate id's and names that look like above?
I will be submitting this all by ajax by using jquery.serializeArray() and in the controller I will have a parameter with the View Model is should bind too.