无法让 jquery $ajax 调用 .NET MVC 2 控制器方法
我是 jquery 和 ajax 的新手 - 似乎无法让它工作!请参阅我的相关问题: 使用Json 和 AjaxLink 在 ASP.NET MVC 2 中切换链接值
这是我的 jquery:
$(function () {
$("div[id^=add]").click(function (ev) {
ev.preventDefault();
updateMe(this.id.split('_')[1], "AddRequirement");
});
$("div[id^=remove]").click(function (ev) {
ev.preventDefault();
updateMe(this.id.split('_')[1], "RemoveRequirement");
});
});
function updateMe(myId, myAction) {
$.ajax({
type: "POST",
url: "AgreementType.aspx/" + myAction,
data: 'aId=' + <%:Model.AgreementType.Id%> + '&rId=' + myId,
dataType: "text",
error: function(request, msg){
alert( "Error upon saving request: " + msg );
},
success: function (data) {
alert(data);
}
});
}
目前我有两个不同的 div。 foreach 循环确定要显示哪一个:
<%if(req.Agreements.Any(r => r.AgreementTypeId == Model.AgreementType.AgreementTypeId))
{%>
<div id="<%:string.Format("remove_{0}", req.Id)%>" class="divLink">Remove</div>
<% }
else
{ %>
<div id="<%:string.Format("add_{0}", req.Id)%>" class="divLink">Add</div>
<%{%>
这是我的控制器操作。非常简单:
public JsonResult AddRequirement(string aId, string rId)
{
string result = "Remove";
//Code to add requirement
return this.Json(result);
}
public JsonResult RemoveRequirement(string aID, string rID)
{
string result = "Add";
//Code to remove requirement
return this.Json(result);
}
}
所有成功函数都需要用内容更新目标的innerHtml,并更改id以匹配。看起来很容易!但我似乎无法弄清楚。 TIA
I'm new to jquery and ajax - just can't seem to get this to work! See my related question: Use Json and AjaxLink to Toggle Link Values in ASP.NET MVC 2
Here's my jquery:
$(function () {
$("div[id^=add]").click(function (ev) {
ev.preventDefault();
updateMe(this.id.split('_')[1], "AddRequirement");
});
$("div[id^=remove]").click(function (ev) {
ev.preventDefault();
updateMe(this.id.split('_')[1], "RemoveRequirement");
});
});
function updateMe(myId, myAction) {
$.ajax({
type: "POST",
url: "AgreementType.aspx/" + myAction,
data: 'aId=' + <%:Model.AgreementType.Id%> + '&rId=' + myId,
dataType: "text",
error: function(request, msg){
alert( "Error upon saving request: " + msg );
},
success: function (data) {
alert(data);
}
});
}
Currently I have a two different divs. A foreach loop determines which one to display:
<%if(req.Agreements.Any(r => r.AgreementTypeId == Model.AgreementType.AgreementTypeId))
{%>
<div id="<%:string.Format("remove_{0}", req.Id)%>" class="divLink">Remove</div>
<% }
else
{ %>
<div id="<%:string.Format("add_{0}", req.Id)%>" class="divLink">Add</div>
<%{%>
Here's my controller action. Pretty simple:
public JsonResult AddRequirement(string aId, string rId)
{
string result = "Remove";
//Code to add requirement
return this.Json(result);
}
public JsonResult RemoveRequirement(string aID, string rID)
{
string result = "Add";
//Code to remove requirement
return this.Json(result);
}
}
All the success function needs to do it update the innerHtml of the target with the contents, and change the id to match. Seems so easy! And yet I can't seem to figure it out. TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后 - 有效的代码。这将允许用户单击一个 div,该 div 将根据该 div 的内容调用不同的控制器方法,实际上允许您在 foreach 循环中切换对象的元素。我确信它可以改进;例如,我可能不需要从控制器方法获取 div 的值,但至少它可以工作。
Javascript
控制器
标记
Finally - the code that works. This will allow the user to click on a div which will call a different controller method based on the contents of that div, in effect allowing you to toggle toggle elements of the object in a foreach loop. I'm sure it could be improved upon; for instance, I probably don't need to get the value of the div from the controller method, but at least it works.
Javascript
Controller
Markup