Ajax调用在MVC中的foreach循环中不起作用

时间:2018-09-17 06:16:00

标签: ajax asp.net-mvc

我正在使用AJAX将数据动态添加到数据库中,并在MVC中使用foreach循环显示它们,我还添加了一个按钮,以使用ajax调用删除这些数据。

HTML / MVC代码:

<div id="divaddrules" class="form-group row">
   @try
      {
         foreach (var item in ViewBag.AdditionalRules)
           {
             <div class="col-sm-10">
                <p style="font-size:large">@item.AdditionalDesc</p>
             </div>
             <div class="col-sm-2">
                <input type="button" onclick="Removeinput(@item.id)" class="text-dark" style="border:none; background-color:transparent" value="X" />
              </div>
            }
       }
       catch (Exception ex){ }

</div>

现在,当我单击“删除”按钮时,它将调用以下JS代码:

function Removeinput(id) {
        var datas = {};
            datas.addId = id
        $.ajax({
            url: "/Rooms/RemoveAdditionalRules",
            type: "GET",
            data: datas,
            success: function (result) {
               alert(result.id);
               $("#divaddrules").load(window.location.href + " #divaddrules");
            },
            error: function (result) {
                alert("Error: " + result.status);
           }
        });
}

并将其传递给此控制器:

[HttpGet]
[Authorize]
public ActionResult RemoveAdditionalRules(int addId)
{
     HouseRules rules = db.HouseRules.Find(addId);
     db.HouseRules.Remove(rules);
     db.SaveChanges();

     return Json(JsonRequestBehavior.AllowGet);
}
  

我在Ajax调用错误中遇到500错误。

有人可以告诉我我做错了什么吗?拜托..我被困在这里。

更新: 随附的屏幕截图:Debug Screenshot

3 个答案:

答案 0 :(得分:0)

编写您的Removeinput函数,如下所示:

function Removeinput(id) {
        $.ajax({
            url: "/Rooms/RemoveAdditionalRules",
            type: "GET",
            data: { addId : id},
            success: function (response) {
               alert(response);
               $("#divaddrules").load(window.location.href + " #divaddrules");
            },
            error: function (result) {
                alert("Error: " + result.status);
           }
        });
}

然后在控制器方法中:

[HttpGet]
[Authorize]
public ActionResult RemoveAdditionalRules(int addId)
{
     AdditionalRules rules = db.AdditionalRules.Find(addId); // Here was the problem. He was pointing to the wrong table that has fixed over team viewer.
     db.AdditionalRules.Remove(rules);
     db.SaveChanges();

     return Json(addId,JsonRequestBehavior.AllowGet);
}

答案 1 :(得分:0)

问题在于db缺少值,在图像中,您要求id 25,但返回null,然后尝试删除传递null值的项目。

因此,在您的情况下,您需要先进行验证,然后再删除或修复丢失的数据:

date,group,myval
2018-07-01,test1,999
2018-07-01,test2,523
2018-07-02,test1,3305
2018-07-02,test2,1290
2018-07-03,test1,9399
2018-07-03,test2,1827
2018-07-04,test1,6982
2018-07-04,test2,2391
2018-07-05,test1,4100
2018-07-05,test2,2081
2018-07-06,test1,0268
2018-07-06,test2,4834
2018-07-07,test1,9571
2018-07-07,test2,6369
2018-07-08,test1,3943
2018-07-08,test2,6483

答案 2 :(得分:-1)

提交您的输入类型,可能有帮助

function deleterelation(id) {
    debugger;
    if (id > 0)
        $.ajax({
            url: "/Relations/Delete/" + id,
            type: "get",
            datatype: "json",
            data: { id: id },
            success: function (response) {
                debugger;
                if (response != null) {
                    

                    $("#txtDName").text(response.name);
                    $("#DRelationId").val(response.id);
                    $("#DeleteRelation").modal("show");
                }
            },
            error: function (response) {

                $("#DeleteRelationLoading").hide();
                $("#DeleteRelation_btn_cancel").show();
                $("#DeleteRelation_btn_save").show();
            }
        });
    else
        toastr.error("Something went wrong");
}

<input type="submit" onclick="Removeinput(@item.id)" class="text-dark" style="border:none; background-color:transparent" value="X" />

如果这不起作用,请告诉我