【发布时间】:2015-08-03 07:05:22
【问题描述】:
所以我有这样的部分观点:
@model QueryQuestionManager.Models.Domain.Answer
<script src="~/Scripts/DynamicList.js"></script>
<div class="editorRow">
@using (Html.BeginCollectionItem("Answers"))
{
<div>
@Html.LabelFor(x => x.AnswerText)
@Html.EditorFor(x => x.AnswerText)
</div>
<a href="#" class="deleteRow">delete</a>
}
</div>
当有人点击链接时,我想在我的主视图中加载这个局部视图:
@model QueryQuestionManager.Models.Domain.Question
@{
ViewBag.Title = "Create";
}
<script src="~/Scripts/DynamicList.js"></script>
<br />
@using (Html.BeginForm(new { @class = "form", role = "form" }))
{
<div class="form-group">
<div class="editor-label">
@Html.LabelFor(m => m.QuestionText)
</div>
<div class="form-control">
@Html.TextBoxFor(m => m.QuestionText)
@Html.ValidationMessageFor(m => m.QuestionText)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Category)
</div>
<div class="form-control">
@Html.TextBoxFor(m => m.Category)
@Html.ValidationMessageFor(m => m.Category)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Answers)
</div>
<br />
<div id="editorRows">
@foreach (var item in Model.Answers)
{
@Html.Partial("EditorRow", item);
}
</div>
@Html.ActionLink("Add another...", "BlankEditorRow", "Question", new { id = "addItem" })
<br />
<input type="submit" value="Save" class="btn btn-success" />
</div>
}
<br />
<div>
@Html.ActionLink("Back to List", "Index", "Home")
</div>
这是控制器的动作结果
public ActionResult BlankEditorRow()
{
return PartialView("EditorRow", new Answer());
}
然后我有一个用于动态添加和删除部分视图的 javascript 文件。
$('#addItem').click(function () {
$.ajax({
url: this.href,
cache: false,
success: function (html) {
$('#editorRows').append(html);
}
});
return false;
});
$('a.deleteRow').live('click', function () {
$(this).parents('div.editorRow:first').remove();
return false;
});
但由于某种原因,我的部分视图没有加载到我当前的视图中,它只打开部分视图。
我是不是忘记了什么?
【问题讨论】:
-
您是否在视图中包含了 unobtrusive-ajax 脚本?通常,当您的局部视图最终出现在“新页面”上时,这意味着请求成功,但缺少 ajax 处理程序,因此它只是将局部视图返回到空白的新页面。编辑:只是想提一下,您可以通过使用 mvc Ajax.ActionLink(...) 帮助程序来缩小问题范围,而不是依赖于正确编码 jquery ajax 方法。
标签: asp.net-mvc razor view partial-views