无法获取的jQuery $ AJAX调用.NET MVC 2控制器的方法 [英] Can't get jquery $ajax to call .NET MVC 2 controller method

查看:121
本文介绍了无法获取的jQuery $ AJAX调用.NET MVC 2控制器的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的jQuery和Ajax - 只是似乎无法得到这个工作!请参阅我的相关的问题:<一href="http://stackoverflow.com/questions/4444852/use-json-and-ajaxlink-to-toggle-link-values-in-asp-net-mvc-2">Use JSON和AjaxLink到肘杆值在ASP.NET MVC 2

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

下面是我的jQuery的:

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);
        }
    }); 
}

目前我有两个不同的div。 foreach循环确定哪一个显示:

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>
    <%{%>

下面是我的控制器操作。 pretty的简单:

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);
    }
}

所有成功的功能需要做更新的内容目标的innerHTML,并更改其ID匹配。似乎如此简单!然而,我似乎无法弄清楚。 TIA

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

推荐答案

最后 - 在code,它的工作原理。这将允许用户点击一个div它会调用基于该分区中的内容不同控制器的方法,实际上允许你切换对象切换元素foreach循环。我敢肯定,它可以改进;举例来说,我也许并不需要从控制器方法的div的价值,但至少它的工作原理。

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的

<script type="text/javascript">
    $(function () {
        $("div[class^=toggleLink]").click(function (ev) {
            ev.preventDefault();

            var divText = $('#' + this.id).text();

            if (divText == "Remove") {
                updateMe(this.id, "Remove");
            }
            else if (divText == "Add") {
                updateMe(this.id, "Add");
            }
        });
    });


function updateMe(myId, myAction) {
    $.ajax(
    {
        type: "POST",
        url: "/AgreementType/" + myAction,
        data: "aId=<%:Model.AgreementType.Id%>&rId=" + myId,
        success: function (result) {
            if (result.success) {
                $('div#' + myId).text(result.value);
            }
        },
        error: function (req, status, error) {
            alert(req + " " + status + " " + error);
        }
    });
}

</script>

控制器

    public ActionResult Add(string aId, string rId)
    {
        //Add to the template

        string result = "Remove";
        string nClass = "remLink";

        return Json(new { success = true, value = result, newClass = nClass });
    }


    public ActionResult Remove(string aID, string rId)
    {
        //Remove from the template

        string result = "Add";
        string nClass = "addLink";

        return Json(new { success = true, value = result, newClass = nClass });
    }

标记

<% foreach(var req in std.Requirements)%>
   <% { %>
   <tr>
       <td>
       <%if(req.Agreements.Any(r => r.AgreementTypeId == Model.AgreementType.Id))
       {%>
           <div id="<%:req.Id%>" class="toggleLink">Remove</div>
       <% }
       else { %>
           <div id="<%:req.Id%>" class="toggleLink">Add</div>
       <%} %>

这篇关于无法获取的jQuery $ AJAX调用.NET MVC 2控制器的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆