私は MVC3 を学習している最中であり@Ajax.Actionlink
、ajax 呼び出しの jQuery メソッドを使用するように変換したい部分的なビューに次のようなものがありますが、よくわからない 2 つの主な問題があります。
- パラメーターを ajax 呼び出しに取得する方法。(userIDは部分ビューのViewModelにあるので問題ないのですが、子コレクションのインスタンスからcustomerIDを取得する方法がわかりません。)
- 部分ビューを指定された div に戻す方法。(これを試してみると、部分ビューにリダイレクトされるだけですが、現在
@Ajax.ActionLink
は div が正しく更新されます。)
以下は(私が思うに)関連するコードのすべてです:
部分的なビュー
@model ..snip../CustomerViewModel
<div id="updatedablediv">
<table class="customers" style="width: 50%">
<thead>
<tr>
<th>
Name
</th>
<th>
Actions
</th>
</tr>
</thead>
<tbody>
@if (Model != null)
{
foreach (Customer item in Model.Customers)
{
<tr id="customer-id-@item.CustomerID">
@Html.HiddenFor(i => item.CustomerID)
<td>
@Html.DisplayTextFor(i => item.Name)
</td>
<td>
@Ajax.ActionLink("Delete", "DeleteCustomer", "MyController", new { userID = Model.UserID, customerID = item.CustomerID },
new AjaxOptions() { UpdateTargetId = "updatedablediv", HttpMethod = "Get" }, new { @class = "standardbutton" })
</td>
</tr>
}
}
</tbody>
</table>
</div>
コントローラ アクション
[HttpGet]
public PartialViewResult DeleteCustomer(int userID, int customerID)
{
try
{
//Delete code
}
catch (DataException)
{
ModelState.AddModelError("", "Unable to save changes.");
}
CustomerViewModel returnViewModel = new CustomerViewModel()
{
UserID = userID,
Customers = repository.Customers
};
return PartialView("CustomerPartial", returnViewModel);
}
これをやってみましたが、上記の問題に悩まされ続けています。私の試みの1つは、次のjQueryでした:
$("#buttonid").click(function(){
var loadUrl = $('#buttonid').attr('href');
$("#updatedablediv")
.html(DeleteCustomer)
.load(loadUrl, {userID: @model.UserID);
});
これをjQueryに変換するのに役立つポインタ@Ajax.ActionLink
は大歓迎です。
どうもありがとう、
HTML の更新
<tr>
これは、私の部分ビューでのインスタンスの html です。
<tr id="customer-id-1003">
<input id="item_CustomerID" name="item.CustomerID" type="hidden" value="1003" />
<td>
James
</td>
<td>
<a class="standardbutton" data-ajax="true" data-ajax-method="Get" data-ajax-mode="replace" data-ajax-update="#updatedablediv" href="/MyController/DeleteCustomer?userID=12&customerID=1003">Delete</a>
</td>
</tr>