ASP.NET MVC:モデルバインディングおよびAJAXリクエスト
-
12-10-2019 - |
質問
ボタンがあります:
<a id="2" class="modalInput specialbutton" href="/Employee/Delete/2" rel="#yesno"><img src="/Content/Images/application_delete.png" alt="Delete" /></a>
ボタンのJavaScript:
var buttons = $("#yesno button").click(function (e) {
var yes = buttons.index(this) === 0;
if (yes) {
$.ajax({
url: overlayElem.attr('href'),
success: function (data) {
$("#gridcontainer").html(data);
}
});
}
});
アクションを削除します:
public ActionResult Delete(int id)
{
DeleteTeamEmployeeInput deleteTeamEmployeeInput = new DeleteTeamEmployeeInput { TeamEmployee = id };
return Command<DeleteTeamEmployeeInput, TeamEmployee>(deleteTeamEmployeeInput,
s => RedirectToAction<EmployeeController>(x => x.Index(1)),
f => RedirectToAction<EmployeeController>(x => x.Index(1)));
}
問題はです id
パラメーター。直接使用するといいでしょう DeleteTeamEmployeeInput
.
public ActionResult Delete(DeleteTeamEmployeeInput deleteTeamEmployeeInput )
{
return Command<DeleteTeamEmployeeInput, TeamEmployee>(deleteTeamEmployeeInput,
s => RedirectToAction<EmployeeController>(x => x.Index(1)),
f => RedirectToAction<EmployeeController>(x => x.Index(1)));
}
complextオブジェクトを使用すると、常にnullです。 Simple Intタイプは正常に機能します。
削除アクションに複雑なタイプを使用するにはどうすればよいですか?
クラスdeleteTeamployeeEinput:
public class DeleteTeamEmployeeInput
{
public int TeamEmployee { get; set; }
}
削除ボタン:
public static string DeleteImageButton(this HtmlHelper helper, int id)
{
string controller = GetControllerName(helper);
string url = String.Format("/{0}/Delete/{1}", controller, id);
return ImageButton(helper, url, "Delete", "/Content/Images/application_delete.png", "#yesno", "modalInput", id);
}
解決
クリックコールバックからfalseを返すことにより、デフォルトのアクション結果をキャンセルする必要があります。オブジェクト全体を送信する限り(単一のみが含まれています TeamEmployee
Integer Propertry)あなたがこれを行うことができる懸念:
// that selector seems strange as you don't have a button inside your anchor
// but an <img>. You probably want to double check selector
var buttons = $('#yesno button').click(function (e) {
var yes = buttons.index(this) === 0;
if (yes) {
$.ajax({
url: this.href,
success: function (data) {
$("#gridcontainer").html(data);
}
// that's what I was talking about canceling the default action
});
return false;
}
});
次に、アンカーを生成して、このパラメーターを含めるようにします。
<a href="<%: Url.Action("delete", "employee", new { TeamEmployee = "2" }) %>" id="link2" class="modalInput specialbutton" rel="#yesno">
<img src="<%: Url.Content("~/Content/Images/application_delete.png") %>" alt="Delete" />
</a>
今、あなたは安全に持つことができます:
public ActionResult Delete(DeleteTeamEmployeeInput deleteTeamEmployeeInput)
{
...
}
述べる: id="2"
アンカーには有効な識別子名がありません。
所属していません StackOverflow