0

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...

Prashanth Thurairatnam
  • 4,353
  • 2
  • 14
  • 17
Nicholas
  • 1,189
  • 4
  • 20
  • 40
  • http://stackoverflow.com/questions/435387/passing-data-from-view-to-controller – joncodo May 21 '12 at 11:08
  • Could you transform that to mycode/C# instead of ASP.NET? – Nicholas May 21 '12 at 11:12
  • seem my DDL tutorials [1]: http://www.asp.net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvc [2]: http://blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net-mvc.aspx – RickAndMSFT May 22 '12 at 19:22

3 Answers3

0

Try using following code

Controller - Make sure you put HttpPost attribute there

 [HttpPost]
 public ActionResult genBlocks(string StartTime,FormCollection collection)
 {
   return View();
 }

Your view -- Make sure you put actual action & controller name in beginform method

@using (Html.BeginForm("genBlocks","ControllerName"))
{
    <input type="text" name="StartTime" />

    <div id="genBlocks" class="button">
        <input type="submit" />
    </div>
}
Pravin Pawar
  • 2,559
  • 3
  • 34
  • 40
0
@using (Html.BeginForm("genBlocks", "Block"))
{
@Html.DropDownList("StartTime", String.Empty)
<input type="submit" class="button" value="Generate blocks" />
}

was the solution for the index :)

Nicholas
  • 1,189
  • 4
  • 20
  • 40
0

Your drop down list is really only sending back a single scalar value - the currently selected value. This can be an int, string, whatever. If it's part of your model, you can use Html.DropDownListFor(m => m.Hour, [INSERT POSSIBLE SELECTITEMLIST ARRAY HERE]).

The other option is to simply accept an int, string, etc on your action method if you need to get only that value.

It seems the issue is being over-worked.

Ryan Peters
  • 7,608
  • 8
  • 41
  • 57