我有一个循环中不同字段的视图。根据模型,它应该创建与循环计数一样多的字段。这是页面的示例:
_________ ___________ _________
|CONNECTOR| |SEARCHINPUT| |Selection|
|_________| |___________| |_________|
________________ ________________
|Add Search Field| |Sub Search Field|
|_B_U_T_T_O_N____| |_B_U_T_T_O_N____|
视图类看起来像:
@model Modelle.Models.SearchModel
<table>
@for (int i = 0; i < (int)Model.SearchCount; i++)
{ <tr>
<th>@if (i > 0)
{<select name="ConnectorList" id="ConnectorList">
<option value="AND" @(Model.ConnectorList.ElementAt(i - 1).Equals("AND") ? "selected" : "")>AND</option>
<option value="OR" @(Model.ConnectorList.ElementAt(i - 1).Equals("OR") ? "selected" : "") >OR</option>
<option value="NAND" @(Model.ConnectorList.ElementAt(i - 1).Equals("NAND") ? "selected" : "") >NAND</option>
<option value="NOR" @(Model.ConnectorList.ElementAt(i - 1).Equals("NOR") ? "selected" : "") >NOR</option>
</select>}
</th>
<th>
<input name="SearchInput" id="SearchInput" type="text" value="@(Model.SearchList.ElementAt(i) as String)" /></th>
<th>
<select name="SelectionList" id="SelectionList">
@for (int j = 0; j < Model.SelectedList.Count(); j++)
{<option value="@j" @(Model.Selection.ElementAt(i).Equals(j) ? "selected" : "")>@(Model.SelectedList.ElementAt(j).Name as String)</option>
}
</select>
</th>
</tr>}
</table>
<p>
<button name="button" type="button" onclick="AddSearchField()">Add one more search field</button>
<button name="button" type="button" onclick="SubSearchField()">Remove one search field</button>
这里是按钮的 jquery 代码:
function AddSearchField() {
var value1 = $('#SearchInput').val();
var value2 = $('#SelectionList').val();
var value3 = $('#ConnectorList').val();
$.ajax({
url: "@Url.Action("AddSearchField", "Search")",
type: 'POST',
chache:false,
data: 'searchInput=' + value1 + '&selectionList=' + value2 + '&connectorList=' + value3,
success: function (response) {
$('#OpenSearchInput').html(response);
},
});
}
function SubSearchField() {
var value1 = $('#SearchInput').val();
var value2 = $('#SelectionList').val();
var value3 = $('#ConnectorList').val();
$.ajax({
url: "@Url.Action("SubSearchField", "Search")",
type: 'POST',
chache: false,
data: 'searchInput=' + value1 + '&selectionList=' + value2 + '&connectorList=' + value3,
success: function (response) {
$('#OpenSearchInput').html(response);
},
});
}
和控制器代码:
public PartialViewResult AddSearchField(List<String> searchInput, List<int> selectionList, List<String> connectorList)
{
SearchModel searchModel = new SearchModel();
if (connectorList == null)
{
connectorList = new List<String>();
}
searchModel.ConnectorList = connectorList;
searchModel.Selection = selectionList.ToList();
searchModel.SelectedList = setSearchFields();
List<String> searchList = new List<String>();
int row = searchInput.Count();
searchModel.SearchList = searchInput.ToList();
searchModel.SearchList.Add("search");
searchModel.SearchCount = row + 1;
searchModel.ConnectorList.Add("AND");
searchModel.Selection.Add(0);
return PartialView("SearchInput", searchModel);
}
public PartialViewResult SubSearchField(List<String> searchInput, List<int> selectionList, List<String> connectorList)
{
SearchModel searchModel = new SearchModel();
if (connectorList == null)
{
connectorList = new List<String>();
}
searchModel.ConnectorList = connectorList;
searchModel.Selection = selectionList.ToList();
searchModel.SelectedList = setSearchFields();
List<String> searchList = new List<String>();
int row = searchInput.Count();
searchModel.SearchList = searchInput.ToList();
if (searchInput.Count() > 1)
{
searchModel.SearchList.RemoveAt(searchInput.Count() - 1);
searchModel.SearchCount = row - 1;
searchModel.ConnectorList.RemoveAt(connectorList.Count() - 1);
searchModel.Selection.RemoveAt(selectionList.Count() - 1);
}
else
{
searchModel.SearchCount = row;
}
return PartialView("SearchInput", searchModel);
}
如果我正常加载页面,它有一个搜索行。如果我按下“再添加一个搜索字段”按钮,它会添加一行。如果我再次单击它,则没有任何反应。如果我单击“删除一个搜索字段按钮(随时)第二个字段消失。在调试模式下,我可以在控制器中看到每次列表只计数 1。
有人知道我错在哪里了吗?知道任何人添加线条的更简单方法吗?我必须稍后将所有输入发送到控制器,以便根据输入在具有不同规则的数据库中进行搜索。