我对MVC和JQuery很新。我正在使用MVC 4.我有一个索引视图,我可以编辑字段。我希望能够在列表底部添加一个空行,用于添加新行。到目前为止,我的工作基于MVC 2中的这个示例:http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
这是我的索引视图:
@using (Html.BeginForm()) {
<h3 style="margin-top: 1px;">
<input type="hidden" name="selectedConditionID" value="@ViewBag.SelectedConditionID" />
<input type="hidden" name="isCore" />
<input type="submit" value="Save" />
@Html.ActionLink("Add Condition Detail", "BlankEditorRow", null, new { id="addItem"})
</h3>
<div class="table" id="editorRows">
@for (var i = 0; i < Model.Count; ++i) {
ViewBag.i = i;
Html.RenderPartial("_ConditionDetailEditorRow", Model[i]);
}
</div>
<div>
@Html.ActionLink("Back to Condition Library", null, "Home")
</div>
}
以下是部分视图:
<div class="editorRow">
@if (@ViewBag.i == 0) {
<div class="header-row">
<div class="header-cell">@Html.DisplayNameFor(model => model.StackRank)</div>
<div class="header-cell">@Html.DisplayNameFor(model => model.ID)</div> ...
<div class="header-cell">Options</div>
</div>
}
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
int i = @ViewBag.i;
<div class="body-cell">
@Html.TextBoxFor(m => m.StackRank)
@Html.ValidationMessageFor(m => m.StackRank)
</div>
<div class="body-cell">
@Html.DisplayFor(m => m.ID)
</div> ...
<div class="body-cell">
@Html.ActionLink("Edit", "Edit", new { id=Model.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=Model.ID })
</div>
}
这是我的索引视图中的JQuery,它应该将空行添加到索引列表的末尾:
$(function () {
$("#addItem").click(function () {
$.ajax({
url: this.href,
cache: false,
success: function (html) { $("#editorRows").append(html); }
});
return false;
});
});
这是控制器方法:
public PartialViewResult BlankEditorRow()
{
ViewBag.i = 0;
return PartialView("_ConditionDetailEditorRow", new Condition_Details());
}
问题在于:当我点击索引页面上的“添加条件详细信息”操作链接时,我只获得了部分视图的页面(... // localhost:56213 / ConfigurationUI / ConditionDetails / BlankEditorRow / addItem)。
我想要的是在索引页面底部显示一个空白行。我假设URL应该是这样的:... // localhost:56213 / ConfigurationUI / ConditionDetails / Index / addItem。
现在我怀疑我的JQuery脚本没有触发,但不知道为什么?
怎么了?
谢谢!
修订后的索引视图
<div id="condition_detailsList">
@{ Html.RenderPartial("_ConditionDetailsEdit", Model); }
</div>
修订了部分视图
<table id="condition_detailsList">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model[0].StackRank)
</th>...
</tr>
</thead>
<tbody id="editorRows">
@for (var i = 0; i < Model.Count; ++i) {
<tr style="margin:0; padding:0; border:0;">
<td> <div>
@Html.TextBoxFor(m => m[i].StackRank)
@Html.ValidationMessageFor(m => m[i].StackRank)
</div> </td>...
</tr>
@Html.HiddenFor(m => m[i].ID, new { @id = "hID" + i })...
}
</tbody>
$(function () {
$('#addItem').click(function () {
$.ajax({
cache: false,
type: "POST",
url: "@(Url.Action("GetNewRow", "ConditionDetails"))",
data: { "conditionID": cID },
success: function (condition_detailsListPlusNewRow) {
//initialize fields...
[HttpPost]
public ActionResult GetNewRow(int conditionID) //, List<Condition_Details> conditionDetails)
{
//you must initial the index view here, then...
//retrieve the list, add a new row and return the partial view
var condition_details = db.Condition_Details.Where(x => x.Condition_ID == conditionID).OrderBy(x => x.StackRank).ThenBy(x => x.ID).ToList();
Condition_Details conditionDetail = new Condition_Details();
List<Condition_Details> condition_detailsPlusNewRow = new List<Condition_Details>();
foreach (var row in condition_details)
{
conditionDetail = row;
condition_detailsPlusNewRow.Add(conditionDetail);
}
Condition_Details newRow = new Condition_Details();
newRow.Condition_ID = conditionDetail.Condition_ID;
newRow.Condition = conditionDetail.Condition;
newRow.StackRank = conditionDetail.StackRank + 1;
condition_detailsPlusNewRow.Add(newRow);
return PartialView("_ConditionDetailsEdit", condition_detailsPlusNewRow);
}