We've been searching for like a day right now and we can't figure out to pass data from our view to our controller. In fact we want the user to select an hour from the dropdownlist on the Index() View as StartTime for a time block. At the moment we have to following code.
Our action in the controller
public ActionResult genBlocks(string StartTime, FormCollection collection)
{
int blocksPerDay = 4;
int strH, strM;
List<Block> blocks = new List<Block>();
for (int i = 0; i < blocksPerDay; i++)
{
if (i != 0)
{
strH = blocks[i-1].EndTime.Hour;
strM = blocks[i-1].EndTime.Minute;
}
else
{
strH = Convert.ToInt32(collection["StartTime"]);
strM = 0;
}
Block block = new Block
{
FieldDayId = 1,
Available = true,
StartTime = DateTime.Today + new TimeSpan(strH, strM, 0),
EndTime = DateTime.Today + new TimeSpan(strH, strM + (db.Sports.Find(1).Duration*(i+1)), 0),
};
blocks.Add(block);
db.Blocks.Add(block);
db.SaveChanges();
}
return RedirectToAction("Index");
}
And in our View I implemented a form
@using (Html.BeginForm("genBlocks"))
{
<input type="text"name="StartTime" />
@Html.DropDownList("StartTime", String.Empty)
<div id="genBlocks" class="button">
<input type="submit" />
</div>
}
So we tried it with the dorpdownlist and also with a textbox but none of them work out...