我正在使用 jQuery/Ajax 调用将部分视图附加到表中。页面加载时,会正确创建局部视图。但是,一旦使用尝试将另一个项目附加到表中,尽管使用了完全相同的局部视图,但格式不正确。
这是桌子。加载后,项目将正确加载到页面上,如下图所示:
<table id="fixedRows">
<thead>
<tr>
<th>State Code</th>
<th>Agent ID</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.BankListAgentId)
{
if (!String.IsNullOrWhiteSpace(item.AgentId) && item.FixedOrVariable.Equals("F"))
{
@Html.EditorFor(model => item, "FixedPartialView")
}
}
</tbody>
</table>
<br />
<a href="#" class="addFixed">Add Another</a>
单击Add another
链接后,将激活此 jQuery/Ajax 调用
<script type="text/javascript">
$(document).ready(function () {
$(".addFixed").click(function () {
//alert('test');
event.preventDefault();
$.ajax({
url: '@Url.Action("BlankFixedRow", "BankListMaster")',
cache: false,
success: function (html) { $("#fixedRows").append(html); }
});
});
$("#addVariable").click(function () {
event.preventDefault();
$.ajax({
url: '@Url.Action("BlankFixedRow", "BankListMaster")',
cache: false,
success: function (html) { $("#variableRows").append(html); }
});
});
});
</script>
那个 jQuery 从控制器调用这个方法
public ViewResult BlankFixedRow()
{
SelectList tmpList = new SelectList(new[] { "AL", "AK", "AS", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FM", "FL", "GA", "GU", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MH", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NA", "NM", "NY", "NC", "ND", "MP", "OH", "OK", "OR", "PW", "PA", "PR", "RI", "SC", "SD", "TN", "TX", "UT", "US", "VT", "VI", "VA", "WA", "WV", "WI", "WY" });
ViewBag.StateCodeList = tmpList;
return View("FixedPartialView", new BankListAgentId());
}
这称之为部分视图编辑(几个人注意到id
标签中缺少<tr>
,这只是这篇文章的复制/粘贴错误,实际代码有id
标签)
@model Monet.Models.BankListAgentId
@{
Layout = null;
}
@using (Html.BeginCollectionItem("BankListAgentId"))
{
<tr id="item-@Model.AgentId">
<td>
@Html.DropDownListFor(model => model.StateCode,
(SelectList)ViewBag.StateCodeList, Model.StateCode)
</td>
<td>
@Html.EditorFor(model => model.AgentId)
@Html.ValidationMessageFor(model => model.AgentId)
</td>
<td>
<a href="#" class="deleteRow">delete</a>
</td>
@*<td><a href="#" onclick="$('#item-@Model.AgentId').parent().remove();" style="float:right;">Delete</a></td>*@
</tr>
}
这是页面首次加载时调用的同一个局部视图,这也是我对点击Add another
链接后的最终结果看起来像这样感到困惑的部分原因
编辑
如果您点击Add another
链接两次,这就是结果
编辑
我尝试了以下 jQuerysucess
命令,但没有成功
success: function (html) { $("#fixedRows > tbody:last").append(html); }
success: function (html) { $("#fixedRows tr:last").after(html); }
success: function (html) { $("#fixedRows > tbody").append(html); }
这是Add another
单击链接后呈现的 HTML。我在<form>
其下方添加了表单的开始标记,以表明无处可找到新行。
<form action="/BankListMaster/Edit/11" method="post">
<fieldset>
<legend>Stat(s) Fixed</legend>
<table id="fixedRows">
<tr>
<th>State Code</th>
<th>Agent ID</th>
<th></th>
<th></th>
</tr>
<tr id="item-1164998320">
<td>
<select id="item_StateCode" name="item.StateCode"><option value="">HI</option>
<option>AL</option>
..
<option>WY</option>
</select>
</td>
<td>
<input class="text-box single-line" id="item_AgentId" name="item.AgentId" type="text" value="1164998320" />
<span class="field-validation-valid" data-valmsg-for="item.AgentId" data-valmsg-replace="true"></span>
</td>
<td>
<a href="#" class="deleteRow">delete</a>
</td>
</tr>
<tr id="item-1164998219">
<td>
<select id="item_StateCode" name="item.StateCode">
<option value="">HI</option>
<option>AL</option>
..
<option>WY</option>
</select>
</td>
<td>
<input class="text-box single-line" id="item_AgentId" name="item.AgentId" type="text" value="1164998219" />
<span class="field-validation-valid" data-valmsg-for="item.AgentId" data-valmsg-replace="true"></span>
</td>
<td>
<a href="#" class="deleteRow">delete</a>
</td>
</tr>
<tr id="item-0352926603">
<td>
<select id="item_StateCode" name="item.StateCode">
<option value="">GA</option>
<option>AL</option>
..
<option>WY</option>
</select>
</td>
<td>
<input class="text-box single-line" id="item_AgentId" name="item.AgentId" type="text" value="0352926603" />
<span class="field-validation-valid" data-valmsg-for="item.AgentId" data-valmsg-replace="true"></span>
</td>
<td>
<a href="#" class="deleteRow">delete</a>
</td>
</tr>
</table>
<br />
<a href="#" class="addFixed">Add Another</a>
</fieldset>
</form>
<a href="#" class="addFixed">Add Another</a>
<form action="/BankListMaster/Edit/11" method="post">
编辑
Add Another
这是单击链接后 Chrome 调试器中表格的屏幕截图。如您所见,从表中提取的数据已正确加载到相应<tr>
的标签中,但是空行(通过与其余部分相同的部分视图发送)没有任何相同的表元素。然而,下面的屏幕截图显示了响应,其中确实包含<tr>
标签
编辑
我console.log(html)
在 Ajax 函数中添加了一行,success
所以它现在显示为
success: function (html) {
console.log(html);
$("#fixedRows > tbody").append(html);
}
这是控制台输出(为便于阅读而编辑的状态)
<input type="hidden" name="BankListAgentId.index" autocomplete="off" value="3f7e0a92-8f20-4350-a188-0725919f9558" />
<tr>
<td>
<select id="BankListAgentId_3f7e0a92-8f20-4350-a188-0725919f9558__StateCode" name="BankListAgentId[3f7e0a92-8f20-4350-a188-0725919f9558].StateCode">
<option>AL</option>
..
<option>WY</option>
</select>
</td>
<td>
<input class="text-box single-line" id="BankListAgentId_3f7e0a92-8f20-4350-a188-0725919f9558__AgentId" name="BankListAgentId[3f7e0a92-8f20-4350-a188-0725919f9558].AgentId" type="text" value="" />
</td>
<td>
<a href="javascript:void(0)" class="deleteRow">delete</a>
</td>
</tr>