ASP.NET MVC和放大器; JQuery的动态表单内容 [英] ASP.NET MVC & JQuery Dynamic Form Content
问题描述
我想动态的字段添加到ASP.NET MVC的形式使用jQuery。
I would like to dynamically add fields to an ASP.NET MVC form with JQuery.
例如:
<script language="javascript" type="text/javascript">
var widgets;
$(document).ready(function() {
widgets = 0;
AddWidget();
});
function AddWidget() {
$('#widgets').append("<li><input type='text' name='widget" + widgets + "'/></li>");
widgets++;
}
</script>
<ul id="widgets">
</ul>
这工作,但我要手动循环在控制器中的表单值:
This works, but I was going to manually iterate the form values in the controller:
[AcceptVerbs("Post")]
public ActionResult AddWidget(FormCollection form)
{
foreach (string s in form)
{
string t = form[s];
}
return RedirectToAction("ActionName");
}
但它发生,我当我发送用户返回到在控制器中付诸行动,我将不得不设置FORMDATA用输入的数值,然后反复以&lt添加小部件;%脚本
But it occurred to me when I send the user back to the Get Action in the Controller I will have to set the FormData with the values entered and then iteratively add the widgets with <% scripting.
什么是EST方式在当前版本中做到这一点(5我相信)?
What is the est way to do this in the current release (5 I believe)?
推荐答案
我的解决方案可能是这样的(伪code):
My solution could be something like this (pseudo-code):
<script language="javascript" type="text/javascript">
var widgets;
$(document).ready(function() {
widgets = 0;
<% for each value in ViewData("WidgetValues") %>
AddWidget(<%= value %>);
<% next %>
});
function AddWidget( value ) {
$('#widgets').append("<li><input type='text' name='widget" + widgets +
"'>" + value + "</input></li>");
widgets++;
}
</script>
<ul id="widgets">
</ul>
和控制器:
[AcceptVerbs("Post")]
public ActionResult AddWidget(FormCollection form)
{
dim collValues as new Collection;
foreach (string s in form)
{
string t = form[s];
collValues.add( t )
}
ViewData("WidgetValues") = collValues;
return RedirectToAction("ActionName");
}
您可以在以后制定出信息
(抱歉用C#混合VB,我是一个VB的家伙)
You can work out the details later
(sorry for mixing VB with C#, I'm a VB guy)
这篇关于ASP.NET MVC和放大器; JQuery的动态表单内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!