如何使用Ajax

时间:2016-05-13 10:19:57

标签: jquery ajax asp.net-mvc asp.net-ajax

我的代码如下:

1.控制器动作

    [HttpPost]
    public JsonResult DeleteSelected(IEnumerable<int> ids)
    {
        var model = new CategoryModel();
        for(var id in ids){
            var res = model.DeleteCategory(id);
            if (res == 1)
            {
               return Json( new
                    {
                        status = true 
                    }
               );  
            }else{
                return Json( new
                    {
                        status = false 
                    }
                );  
            }
        }
    }

2.jQuery事件

$('a.btn-del-all').off('click').on('click', function (e) {
        e.preventDefault();
        var boxData = [];
        $("input[name='table_records']:checked").each(function () {
            boxData.push($(this).val());
        });
        $.ajax({
            url: "/backend/category/DeleteSelected",
            type: "POST",
            dataType: "json",
            data: { ids: boxData.join(",") },
            success: function (response) {
                if (response.status == true) {
                    alert("Delete Success...");
                } else {
                   alert("Delete Error...");
                }
            },
            error: function () {
                alert("Error loading data...");
            }
        });
    });

3.View

<a href="#" class="icon-bar btn-del-all" title="Dell all"><i class="fa fa-trash"></i> 
</a>
 @foreach (var item in Model)
    {
     <tr id="item-@item.ID">
     <td class="a-center ">
        <input type="checkbox" class="flat" name="table_records" value="@item.ID">
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.name)
   </td>
   <td>
        @Html.DisplayFor(modelItem => item.slug)
   </td>
   <td>
       @Html.DisplayFor(modelItem => item.parent)
   </td>
   <td>
       @Html.DisplayFor(modelItem => item.lang)
   </td>
    <td>
         @Html.DisplayFor(modelItem => item.ordering)
     </td>
 </tr>
 }

我得到的错误

  

第1行:&lt;%@ Application Codebehind =“Global.asax.cs”Inherits =“WebApp.MvcApplication”Language =“C#”%&gt;

0 个答案:

没有答案