weixin_33698043 2015-03-11 06:51 采纳率: 0%
浏览 4

使用ajax的呼叫控制器

hey every one i want to call controller action using ajax. but its gives an error. "resource cannot be found". javascript function call successfully controller action is not call

here is my view code:

<a class="btn btn-primary" onclick="EditUser('Delete', 'Users', '@Model.Items.ElementAt(i).ID');">Link</a>



 function EditUser(action, controller, id)
            {

                alert(action);
                alert(controller);
                alert(id);

                $.ajax({
                    type: "POST",
                    url: '@Url.Action("Edit", "Users")',
                    data: "{ id: '" + id + "'}",
                    success: function (response) {
                    if (response.d != '1') {
                        alert('Not Saved!');
                    }
                    },
                    error: function (response) {
                    if (response.responseText != "") {
                         alert(response.responseText);
                         alert("Some thing wrong..");
                       }
                   }
                 });
            }

and here is my controller action.

public override ActionResult Edit(int id = 0)
        {
    int a = id;
     return View("Edit", DTO);
    }
  • 写回答

1条回答 默认 最新

  • weixin_33709364 2015-03-11 06:58
    关注
    <div id='Sample'></div>
    Modify your Ajax function as below.
    $.ajax({
                type: "POST",
                url: '@Url.Action("Edit", "Users")',
                data: { id: id },
                success: function (response) {
                $('#Sample').html(response);
                },
                error: function (response) {
                if (response.responseText != "") {
                     alert(response.responseText);
                     alert("Some thing wrong..");
                   }
               }
             });
    
    [HttpPost]
    public override ActionResult Edit(int id = 0)
        {
    int a = id;
     return PartialView("Edit",dto);
    }
    

    if you are specifying the Ajax type as post make sure to add
    [HttpPost] attribute in controller action.

    评论

    报告相同问题?

    悬赏问题

    • ¥30 关于#开发语言#的问题:我需要在抄板的基础上再抄板抄程序,根据RDA8851CM基础上开发
    • ¥15 oracle 多个括号,怎么删除指定的括号及里面的内容?
    • ¥15 poly函数表达式的看法
    • ¥15 我想问一下有人熟悉思科packet tracer
    • ¥15 小新14API2019想用bios调风扇
    • ¥15 Python和易语言爬虫探讨
    • ¥15 有Sakai 会吗,没做过,需要交
    • ¥20 使用vba登录公司统一登录的网页
    • ¥15 opensea上传人机验证不了
    • ¥15 RK3568 kernel5.10