My question is similar to Engram’s here, but my question goes a bit further. The way i intend it to work is I have a textbox asking how many entries a user is going to make. After they input the number, I need to create that many more textboxes to allow for entries (and then repeat the same process with those textboxes, but baby steps first…) I tried collecting the keys on the post, but it only returns the initial textbox asking for the number of entries. I’m still trying to get a grasp on MVC and the tutorials/videos so far don’t delve this deep into it yet. Then again, I know this is probably something I could handle using JQuery, but I’d still be stuck in the same situation.
This is the controller I’m using:
[AcceptVerbsAttribute("POST")] public ActionResult Create(int tbxNumberOfExercises) { ViewData["number"] = tbxNumberOfExercises; foreach (var key in Request.Form.Keys) { string keyString = key.ToString(); if (keyString.StartsWith("tbox_exercise", StringComparison.OrdinalIgnoreCase)) { string recNum = keyString.Substring(13, keyString.Length - 13); string approvedKey = Request.Form["tbox_exercise" + recNum]; int number; int.TryParse(approvedKey, out number); } } return View("Create"); }
And this is my aspx:
<form action="/CreateWorkout/Create" method="post"> Number of Exercises: <%= Html.TextBox("tbxNumberOfExercises") %> <br /> <br /> <input type="submit" value="Set Exercise Number" /> </form> <% if (ViewData["number"] != null)%> There are this many:<%=Html.Encode(ViewData["number"])%> <br /> and this line should show up <% if (ViewData["number"] != null) { int max = (int)ViewData["number"]; for (int i = 0; i < max; i++) {%> <br /> <br /> <%= Html.TextBox("tbox_exercise" + i) %> <% } } %> <% if (ViewData["s"] != null) %> <%=Html.Encode(ViewData["s"]) %>
Is there something I’m overlooking, not comprehending, or should I quit while I’m at it because it seems like I’ll never get it?
Thanks in advance for any help — I’m just trying to learn as most I can.
I’d break this up in stages, you’ll need to add a ‘Save’ view someplace depending on what you want.
Scott
And then in your controller something like this: