将@Ajax.ActionLink转换为jQuery
我正在学习MVC3,我有以下部分视图将@Ajax.ActionLink转换为jQuery,jquery,ajax,asp.net-mvc-3,Jquery,Ajax,Asp.net Mvc 3,我正在学习MVC3,我有以下部分视图@Ajax.Actionlink,我想将其转换为使用Ajax调用的jQuery方法,但是我有两个主要问题,我不能完全弄清楚: 如何将参数转换为ajax调用。(userID在部分视图的ViewModel中很好,但我看不出如何从子集合的实例中获取customerID。) 如何将部分视图返回到指定的div中。(当我尝试此操作时,它只是重定向到部分视图,而当前的@Ajax.ActionLink确实正确地更新了div。) 以下是所有(我认为)相关代码: 局部视图 @
@Ajax.Actionlink
,我想将其转换为使用Ajax调用的jQuery方法,但是我有两个主要问题,我不能完全弄清楚:
- 如何将参数转换为ajax调用。(userID在部分视图的ViewModel中很好,但我看不出如何从子集合的实例中获取customerID。)
- 如何将部分视图返回到指定的div中。(当我尝试此操作时,它只是重定向到部分视图,而当前的
确实正确地更新了div。)@Ajax.ActionLink
@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>
我曾经尝试过这样做,但我总是遇到上面提到的问题。我的一次尝试是使用以下jQuery:
$("#buttonid").click(function(){
var loadUrl = $('#buttonid').attr('href');
$("#updatedablediv")
.html(DeleteCustomer)
.load(loadUrl, {userID: @model.UserID);
});
如果有任何能帮助我将这个@Ajax.ActionLink
转换成jQuery的指针,我将不胜感激
非常感谢
更新HTML
这是我的局部视图中
实例的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>
詹姆斯
生成链接的属性为您提供了正确执行AJAX请求所需的一切:
让我们这样做:
$('.standardbutton[data ajax=“true”])。单击(函数(e){
e、 预防默认值();
var$this=$(this);
$.ajax({
方法:$this.data('ajaxMethod').toUpperCase(),
cache:false,
url:$this.attr('href'),
数据类型:“html”,
成功:功能(resp){
//这假设数据ajax模式始终为“替换”:
$($this.data('ajaxUpdate')).html(resp);
}
});
});
我忘了添加默认值,jQuery的那一部分来自内存!问题仍然存在,我不知道如何从模型的子对象传入第二个参数。谢谢。如果您在浏览器中发布HTML代码,这将非常有帮助。这将允许任何不熟悉ASP.NET的人正确回答您的问题。我已经按照您的建议做了,并发布了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>