我認爲這將是一個不錯的主意,並嘗試使用您嘗試編輯的數據來實現編輯表格,以替換表格中的位置。在創建表單工作正常且功能正常的情況下,我無法讓表單提交,下面是我需要幫助的代碼。 這是編輯表單的局部視圖。無法提交編輯表格
@using (Html.BeginForm("", "", FormMethod.Post, new { id = "PublisherEditForm" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.PublisherID)
@*@Html.LabelFor(model => model.PublisherName, htmlAttributes: new { @class = "control-label col-md-2" })*@
<td>
@Html.EditorFor(model => model.PublisherName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PublisherName, "", new { @class = "text-danger" })
</td>
@*@Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })*@
<td>
@Html.EditorFor(model => model.City, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })
</td>
@*@Html.LabelFor(model => model.State, htmlAttributes: new { @class = "control-label col-md-2" })*@
<td>
@Html.EditorFor(model => model.State, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.State, "", new { @class = "text-danger" })
</td>
<td>
<input id="saveUpdate" type="submit" value="Update Publisher" class="btn btn-primary" />
</td>
}
這裏是我使用的嘗試,並提交表單的Ajax:
$('#PublisherEditForm').submit(function (e) {
var formData = $(this).serializeArray();
e.preventDefault();
$('MessageContent').
html("<div class='alert alert-info'>Please Wait...</div>");
$.ajax({
url: "@Url.Action("AjaxEdit", "PublishersEF")",
type: "POST",
data: formData,
dataType: "json",
success: function (data) {
$('#MessageContent').html("<div class='alert alert-success'>Your record was updated!</div>");
$('#PublisherEditForm')[0].reset();
var row =
'<tr><td>' + data.PublisherName +
'</td><td>' + data.City +
'</td><td>' + data.State +
'</td><td> <a href="@Url.Action("Index", "PublishersEF")">Refresh View</a></td></tr>';
$('#Publisher' + data.PublisherID).replaceWith(row);
console.log('success');
$('PublisherEdit').hide();
$('#MessageContent').html('<div class="alert alert-warning">There was an error processing your update, please try again or contact the site administrator</div>');
},
error: function (e) {
console.log('error');
$('#MessageContent').html('<div class="alert alert-warning">There was an error processing your update, please try again or contact the site administrator</div>');
}
});
});
我試圖把的console.log的成功和錯誤內外,我沒有看到任何他們在控制檯的
編輯:這裏是C#方法:
[HttpGet]
public PartialViewResult PublisherEdit(int id)
{
Publisher publisher = UnitOfWork.PublisherRepository.Find(id);
return PartialView(publisher);
}
[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult PublisherEdit(Publisher publisher)
{
UnitOfWork.PublisherRepository.Update(publisher);
UnitOfWork.Save();
return Json(publisher);
}
我可以確認的UnitOfWork功能正常。所有這些功能都會連接到數據庫並更新/保存信息。這在以前的版本中
請發表c#方法。 –
控制檯中是否有任何錯誤? –
在控制檯中沒有錯誤,在ajax函數中,我在成功和錯誤中都放置了console.log,並且控制檯中沒有顯示任何東西。我知道當我調用調試工具時,它顯示結束窗體標籤位於任何文本框之前,但是創建窗體看起來應該是相同的並且功能應該如此。 – feare56