ASP .NET MVC 动态创建的字段未绑定到 FormCollection
ASP .NET MVC dynamically created fields not bind to FormCollection
我创建了一个包含动态创建字段的局部 Razor 视图:
for (int i = 0 ; i < atr.Count; i++)
{
<div class="col-md-12 panel panel-default sx-box-shadow-on-hover">
<div class="panel-heading">
<h2 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#@string.Format("{0}{1}", "collapse", @atr[i].Id)" aria-expanded="true">
@atr[i].Label
<span class="caret"></span>
</a>
</h2>
</div>
<div id=@string.Format("{0}{1}", "collapse", @atr[i].Id) class="col-md-12 panel-collapse collapse" aria-expanded="true">
<form>
<div class="form-group">
@switch (atr[i].AtrType)
{
case "TXT":
case "NUM":
<input id=@atr[i].Id class="form-control margin5" name=@atr[i].Name type="text" />
break;
case "CHB":
<input class="form-control margin5" name=@atr[i].Name type="checkbox" />
break;
case "DTP":
@(Html.Kendo().DatePicker()
.Name(@atr[i].Name)
.Value(DateTime.Now)
.HtmlAttributes(new { style = "margin-top: 5px; width:100% !important", title = @atr[i].Name })
)
break;
case "WTXT":
<textarea name=@atr[i].Name , rows="3"></textarea>
break;
}
</div>
</form>
</div>
</div>
}
部分视图被放置在具有静态字段和按钮的其他视图中。当我使用按钮进行 POST 操作时,在 FormCollection 中我只有来自静态字段的值。我无法将此字段绑定到模型,因为用户可以随时添加或删除它们。在局部视图中创建的每个动态字段都有自己的 ID 和名称。我在这里错过了什么?
Post 操作:
[HttpPost]
[Authorize]
public ActionResult Search(FormCollection form)
{
//
}
剃须刀主页:
@using (Html.BeginForm("Search", "Home", FormMethod.Post))
{
<div class="col-md-12 panel panel-default sx-box-shadow-on-hover">
<div class="panel-heading">
<h2 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#searchCollapse" aria-expanded="true">
Search options
<span class="caret"></span>
</a>
</h2>
</div>
<div id="searchCollapse" class="col-md-12 panel-collapse collapse in" aria-expanded="true">
<div class="form-group">
<label class="col-md-12 fontBold margin5">Document type:</label>
@(Html.Kendo().DropDownList()
.Name("DocumentTypesDropDownList")
.DataTextField("Name")
.DataValueField("Id")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetDocTypes", "Home");
});
})
.Events(e =>
{
e.Change("docTypeOnChange");
})
.HtmlAttributes(new { style = "margin-top: 5px; width:100% !important", title = "DocumentTypesDropDownList" })
)
<button id="SearchButton" class="btn btn-primary form-control margin5">Search</button>
<div id="searchPanelDiv">
@{
Html.RenderPartial("SearchPanel");
}
</div>
</div>
</div>
</div>
}
请求正文如下所示:
DocumentTypesDropDownList=1&Identificator=1234
缺少动态字段的参数。
我想如果你把这个:
[HttpPost]
[Authorize]
public ActionResult Search(FormCollection form)
{
var fields = new List<(string, string)>();
foreach (var item in Request.Form)
{
fields.Add((item.Key, item.Value));
}
// do some other stuff
}
在您的控制器中,您可以读取正文请求中的所有字段,无论它们是否动态添加。
HTML 不支持嵌套表单。
如果您从局部视图中删除 <form>
和 </form>
标签,您应该能够从 FormCollection
中捕获由@using (Html.BeginForm
.
我创建了一个包含动态创建字段的局部 Razor 视图:
for (int i = 0 ; i < atr.Count; i++)
{
<div class="col-md-12 panel panel-default sx-box-shadow-on-hover">
<div class="panel-heading">
<h2 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#@string.Format("{0}{1}", "collapse", @atr[i].Id)" aria-expanded="true">
@atr[i].Label
<span class="caret"></span>
</a>
</h2>
</div>
<div id=@string.Format("{0}{1}", "collapse", @atr[i].Id) class="col-md-12 panel-collapse collapse" aria-expanded="true">
<form>
<div class="form-group">
@switch (atr[i].AtrType)
{
case "TXT":
case "NUM":
<input id=@atr[i].Id class="form-control margin5" name=@atr[i].Name type="text" />
break;
case "CHB":
<input class="form-control margin5" name=@atr[i].Name type="checkbox" />
break;
case "DTP":
@(Html.Kendo().DatePicker()
.Name(@atr[i].Name)
.Value(DateTime.Now)
.HtmlAttributes(new { style = "margin-top: 5px; width:100% !important", title = @atr[i].Name })
)
break;
case "WTXT":
<textarea name=@atr[i].Name , rows="3"></textarea>
break;
}
</div>
</form>
</div>
</div>
}
部分视图被放置在具有静态字段和按钮的其他视图中。当我使用按钮进行 POST 操作时,在 FormCollection 中我只有来自静态字段的值。我无法将此字段绑定到模型,因为用户可以随时添加或删除它们。在局部视图中创建的每个动态字段都有自己的 ID 和名称。我在这里错过了什么?
Post 操作:
[HttpPost]
[Authorize]
public ActionResult Search(FormCollection form)
{
//
}
剃须刀主页:
@using (Html.BeginForm("Search", "Home", FormMethod.Post))
{
<div class="col-md-12 panel panel-default sx-box-shadow-on-hover">
<div class="panel-heading">
<h2 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#searchCollapse" aria-expanded="true">
Search options
<span class="caret"></span>
</a>
</h2>
</div>
<div id="searchCollapse" class="col-md-12 panel-collapse collapse in" aria-expanded="true">
<div class="form-group">
<label class="col-md-12 fontBold margin5">Document type:</label>
@(Html.Kendo().DropDownList()
.Name("DocumentTypesDropDownList")
.DataTextField("Name")
.DataValueField("Id")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetDocTypes", "Home");
});
})
.Events(e =>
{
e.Change("docTypeOnChange");
})
.HtmlAttributes(new { style = "margin-top: 5px; width:100% !important", title = "DocumentTypesDropDownList" })
)
<button id="SearchButton" class="btn btn-primary form-control margin5">Search</button>
<div id="searchPanelDiv">
@{
Html.RenderPartial("SearchPanel");
}
</div>
</div>
</div>
</div>
}
请求正文如下所示:
DocumentTypesDropDownList=1&Identificator=1234
缺少动态字段的参数。
我想如果你把这个:
[HttpPost]
[Authorize]
public ActionResult Search(FormCollection form)
{
var fields = new List<(string, string)>();
foreach (var item in Request.Form)
{
fields.Add((item.Key, item.Value));
}
// do some other stuff
}
在您的控制器中,您可以读取正文请求中的所有字段,无论它们是否动态添加。
HTML 不支持嵌套表单。
如果您从局部视图中删除 <form>
和 </form>
标签,您应该能够从 FormCollection
中捕获由@using (Html.BeginForm
.