I'm doing a class lab which consists of designing a little Crowdfunding website. When the user creates a project, he is asked for how many tiers of rewards he wants for his project. If this amount is superior to 0, he's redirected to the Reward/Create page. The "Create" view has a form in it which is generated according to the amount of tiers the user wants.
The problem is that I have no idea how can I link each instance of the "for" loop to a particular "RewardViewModel" object with one single submit button, and how to pass all these objects to the "post" method in the controller.
Here is the controller "Get" method (I know I'll probably have to change the model binded to the view):
public ActionResult Create(int projectid, int amountOfTiers )
{
ViewBag.AmountOfTiers = amountOfTiers;
ViewBag.ProjectTitle = (from p in context.Projects where p.ProjectId == projectid select p).FirstOrDefault().Title;
RewardViewModel model = new RewardViewModel { ProjectId = projectid };
return View(model);
}
And here is the view :
@model CrowdFundMe.ViewModels.RewardViewModel
@{
ViewBag.Title = "Create";
}
<body>
@section Login{
@Html.Action("_Login", "User")
}
<h2>Create</h2>
<p>You are adding rewards for the project @ViewBag.ProjectTitle</p>
@using (Html.BeginForm("Create", "Reward", FormMethod.Post))
{
var u = ViewBag.AmountOfTiers;
for (int i = 0; i < u; i++)
{
var tier = i+1;
@Html.HiddenFor(model => model.ProjectId)
@Html.LabelFor(model => model.Tier, "Tier")<br />
@Html.TextBoxFor(model => model.Tier, new {@Value = tier, @readonly="readonly"})
<br />
<br />
@Html.LabelFor(model => model.Title, "Title")<br />
@Html.TextBoxFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
<br />
<br />
@Html.LabelFor(model => model.AmountOfItems, "Quantity available (leave blank if unlimited)")<br />
@Html.TextBoxFor(model => model.AmountOfItems)
@Html.ValidationMessageFor(model => model.AmountOfItems)
<br />
<br />
@Html.LabelFor(model => model.Description, "Description")<br />
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
<br />
<br />
}
<input type="submit" value="Create" />
}