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.

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建