私はそれをお勧めしたいと思って、編集しようとしているデータを使ってテーブル内の場所を編集フォームで実装しようとします。私は、私のフォームを提出することができませんでしたが、作成フォームは同じ機能を果たし、ここでは助けが必要なコードです。 これは編集フォームの部分図です。ここで編集フォームを提出することができません
@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>
}
は、私は、フォームを試してみて、提出することを使用していますということアヤックスです:
$('#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を置くことを試みていると私はどちらか見ませんでしたコンソールにおけるそれらの
EDIT:
[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#の方法があります正しく機能します。この機能はすべてデータベースに接続し、情報を更新/保存します。次のように
はあなたのボタンを宣言します:
<input id="saveUpdate" value="Update Publisher" type="button" class="btn btn-primary" />
そして、これは機能をJS追加これは私が問題のようなものに直面してきた、と私はそれがこれをやって解決し、以前のバージョンで
投稿方法はC#です。 –
コンソールにエラーがありますか? –
コンソールにエラーはありません。ajax関数でconsole.logを成功とエラーの両方に入れ、コンソールに何も表示されていません。私は、クローズフォームタグがテキストボックスの前にあることを示すデバッグツールをプルアップすると知っていますが、作成フォームは同じで機能します。 – feare56