填充表单中动态的基础上在ASP.Net MVC用户输入 [英] Populating a form dynamically based on user input in ASP.Net MVC
问题描述
我的问题是相似的印迹的<一个href=\"http://stackoverflow.com/questions/223149/how-do-you-handle-the-output-of-a-dynamically-generated-form-in-aspnet-mvc\">here,但我的问题去远一点。我打算它的工作方式是我有一个文本框,询问用户是多少条目要作。他们输入号码后,我需要创建更多的文本框,允许条目(然后这些文本框重复同样的过程,但宝宝第一步骤...)我试图收集后的按键,但它只返回初始文本询问条目的数量。我还在试图让MVC和教程的把握/视频到目前为止不钻研这种深入到它。再说,我知道这可能是我的东西可以处理使用JQuery,但我还是会停留在同样的情况。
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.
这是我使用的控制器:
[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:
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"]) %>
有我俯瞰,不是COM prehending的东西,或者我应该退出,而我在这,因为它好像我永远不会得到它?
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.
斯科特
<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>
然后在你的控制器是这样的:
And then in your controller something like this:
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
}
}
这篇关于填充表单中动态的基础上在ASP.Net MVC用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!