无法让 jquery $ajax 调用 .NET MVC 2 控制器方法

发布于 2024-10-08 08:06:50 字数 1881 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只怪假的太真实 2024-10-15 08:06:50

最后 - 有效的代码。这将允许用户单击一个 div,该 div 将根据该 div 的内容调用不同的控制器方法,实际上允许您在 foreach 循环中切换对象的元素。我确信它可以改进;例如,我可能不需要从控制器方法获取 div 的值,但至少它可以工作。

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

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>

Controller

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

Markup

<% 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>
       <%} %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文