我的问题是相似的印迹 在这里,, 但我的问题走远一点。我打算给它的工作我有一个文本要求如何,许多条目的用户,是要去做。之后他们输入的数量,我需要建立更多的文本框允许的条目(再重复同样的过程中与这些文本框,但是婴儿的步骤的第一个...)我试图收集键职位,但它只是返回的最初文本框中要求的数量的项目。我仍然在试图获得掌握在视和程序/视频迄今为止不深入研究这个深入到它。再说,我知道这可能是我可以处理使用JQuery,但我仍然被困在同样的情况。

这个控制器也是我使用:

[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");
    }

这是我的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"]) %>

是不是有什么我可以俯瞰中,不理解,或者我应该退出,而我在这是因为好像我永远不会得到它的?

预先感谢任何的帮助--我只是想要学习,因为大多数我可以。

有帮助吗?

解决方案

我会打破这个阶段,则需要增加一个"节省"查某个地方,这取决于你想要什么。

斯科特

<form action="/Demo01/Create" method="post">
Number of Exercises:
<%= Html.TextBox("tbxNumberOfExercises") %>
<br />
<br />
<input type="submit" value="Set Exercise Number" />
</form>
<% if (ViewData["number"] != null) {%>
<form action="/Demo01/Save" method="post">
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"]) %>
<input type="submit" value="Save Exercises" />
<% } %>
</form>

然后在你的控制器像这样的东西:

public class Demo01Controller : Controller {
    public ActionResult Create() {
        return View();
    }

    [AcceptVerbsAttribute("POST")]
    public ActionResult Create(int tbxNumberOfExercises) {
        ViewData["number"] = tbxNumberOfExercises;
        return View("Create");
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Save() {
        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(); // return/redirect to wherever you want
    }
}

其他提示

我会考虑增加文本框客户端通过javascript而不是回发给的服务器,以有形式重新绘制,假设你可以住javascript作为一个要求使用的应用程序。如果没有,那么@*斯科特的办法应工作。作为一个优先事项,我可能会有节省的方法采取FormCollection参数,而不是处理请求的对象。

Javascript解决办法将是要有一个单一的文本框和一个按钮增加另一个。用户可能继续增加本框直到他们有足够的。

问题是你的 </form> 结束标签的需求,来结束你的看法。

试试这个改观:

<form action="/CreateWorkout/Create" method="post">
Number of Exercises:
<%= Html.TextBox("tbxNumberOfExercises") %>
<br />
<br />
<input type="submit" value="Set Exercise Number" />
<% 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"]) %>
</form>

我会推荐*斯科特的办法,尽最佳做法。这答案是关于得到你的确切情况的工作。

谢谢你的帮助的家伙。我意识到,在5今天上午,我的问题是,这种形式没有包括的新的文本框/我需要另一种形式。我会必须认真考虑Javascript和实际上修改该DOM,因为它将是更好地保持它。

感谢这么多。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top