我在學習如何使用ajax調用與顯示partialview的引導模態對話框。我創建了一個簡單的MVC 5應用程序。我可以從我父視圖上PERSON1編輯按鈕模態對話框OK即MVC 5使用部分視圖的JQuery Bootstrap模態
- 點擊內編輯記錄,調用編輯(GET)控制器動作,並顯示與PERSON1細節模式。
- 如果我更改年齡值並單擊保存,它會調用編輯(發佈)控制器操作並更新Person1的年齡。
我可以爲Person2做同樣的事情。
但是,如果我試圖再次編輯相同的記錄(即Person1),它將顯示模式對話框,但它不會從ajax get調用控制器操作,並顯示Person2的數據,即我編輯的最後一條記錄。
我做錯了什麼?我已經在下面發佈了我的代碼。
父視圖(index.cshtml)
@using WebApplication1.Models;
@model List<PersonViewModel>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<table>
@foreach (var item in Model)
{
<tr>
<td id="[email protected]">
@Html.Partial("ListItem", item)
</td>
</tr>
}
</table>
<div class="modal" id="editor-container" tabindex="-1" role="dialog" aria-labelledby="editor-title">
<div class="modal-dialog" role="document">
<div class="modal-content" id="editor-content-container"></div>
</div>
</div>
@section scripts
{
<script type="text/javascript">
$(function() {
$('.editor-container').click(function() {
var pid = $(this).attr('data-id');
var url = "/Person/Edit/" + pid;
$.get(url, function (data) {
$('#editor-container').modal('show');
$('#editor-content-container').html(data);
});
});
$('#editor-container').on('hidden.bs.modal', function() {
$(this).removeData('bs.modal');
});
});
function success(data, status, xhr) {
$('#editor-container').modal('hide');
}
function failure(xhr, status, error) {
$('#editor-container').modal('show');
}
</script>
}
莫代爾PartialView(Edit.cshtml)
@using WebApplication1.Models;
@model PersonViewModel
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="editor-title">Edit Person</h4>
</div>
@using (Ajax.BeginForm("Edit", "Person", FormMethod.Post, new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST",
UpdateTargetId = "editor-success-" + @Model.id,
OnSuccess = "success",
OnFailure = "failure",
}))
{
@Html.ValidationSummary()
@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.id)
<div class="modal-body">
<div class="form-group">
@Html.LabelFor(model => model.name)
@Html.EditorFor(model => model.name)
</div>
<div class="form-group">
@Html.LabelFor(model => model.age)
@Html.EditorFor(model => model.age)
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Save</button>
</div>
}
列表項PartialView(ListItem.cshtml)
@using WebApplication1.Models;
@model PersonViewModel
@{
var item = Model;
}
<div class="row">
<div class="col-md-5">@item.name</div>
<div class="col-md-5">@item.age</div>
<button type="button" class="btn editor-container" data-id="@item.id"
data-toggle="modal" data-target="#editor-container">
Edit
</button>
</div>
控制器(PersonController.cs)
public class PersonController : Controller
{
// GET: Person
public ActionResult Index()
{
return View(GetPersons());
}
[HttpGet]
public ActionResult Edit(int id)
{
PersonViewModel p = GetPersons().Find(m => m.id == id);
return PartialView("Edit", p);
}
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Edit(PersonViewModel p)
{
if (TryValidateModel(p))
{
return PartialView("ListItem", p);
}
Response.StatusCode = 400;
return PartialView("Edit", p);
}
private List<PersonViewModel> GetPersons()
{
List<PersonViewModel> plist = new List<PersonViewModel>();
PersonViewModel p = new PersonViewModel()
{
id = 1,
name = "Person1",
age = 33,
};
plist.Add(p);
p = new PersonViewModel()
{
id = 2,
name = "Person2",
age = 30,
};
plist.Add(p);
return plist;
}
}
感謝您的答覆。我試過了你的建議,但我仍然有同樣的問題。 –