MVC jQuery删除操作不起作用

时间:2014-01-22 14:59:17

标签: c# jquery asp.net-mvc

我已经在这一段时间里突破了一段时间。我有下面的表格,工作正常。

@using (Html.BeginForm("Edit", "PCLLine", FormMethod.Post))
{
  @Html.AntiForgeryToken()
  @Html.ValidationSummary(true)

  @Html.HiddenFor(model => model.ID)
  @Html.HiddenFor(model => model.Field)
  <div class="small">
    @Html.TextBoxFor(model => model.Number, new { onchange = "setEditWBSElementNumber(this," + Model.ID + ")" })
    @Html.ValidationMessageFor(model => model.Number)
  </div>

  <div class="large">
    @Html.EditorFor(model => model.Description)
    @Html.ValidationMessageFor(model => model.Description)
  </div>

  <div>
    @Html.EditorFor(model => model.NumberTwo)
    @Html.ValidationMessageFor(model => model.NumberTwo)
  </div>

  <div>
    @Html.TextBoxFor(model => model.ConcatNumber, new { @id = Model.ID.ToString() + "_WBSElementNumber" })
    @Html.ValidationMessageFor(model => model.ConcatNumber)
  </div>

  <div class="large">
    @Html.EditorFor(model => model.Remarks)
    @Html.ValidationMessageFor(model => model.Remarks)
  </div>
  <div>
    <input type="submit" class="accept" value="Opslaan" />
  </div>
  <div>
    <input class="folderButton" value="Opties" onclick="pclSubLineDialog()" />
  </div>
  <div>
    <input class="delete" value="Verwijderen" onclick="deleteItem(); return false;" />
  </div>
}

这里的问题在于删除。它调用函数,但没有正确执行。它没有点击我的Controller操作,但是尝试使用其他控制器操作,它确实有效:

function deleteItem() {
    $.ajax({
      url: "PCLLine/Delete",
      type: "POST",
      data: { id: "@Model.ID" },
      dataType: "json",
      success: function (data) {
        alert('');
      }
    });
  }

控制器:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Delete(int pclLineId)
{
  try
  {
    // TODO: Add delete logic here
    _pclLineBLL.DeletePCLLine(pclLineId);
    return Json(new { success = true });
  }
  catch
  {
    return Json(new { success = false });
  }
}

3 个答案:

答案 0 :(得分:3)

参数名称很重要。您应该根据操作声明重命名您的请求参数:

data: { pclLineId: "@Model.ID" },

答案 1 :(得分:2)

在Controller类中,您已将参数pclLineId命名为id

你可以:

  1. 重命名参数id
  2. 更改您的jQuery函数以传递pclLineId而不是id

答案 2 :(得分:0)

//将您的功能更改为

function deleteItem() {
    $.ajax({
      url: "PCLLine/Delete",
      type: "POST",
      data: { pclLineId: "@Model.ID" },
      dataType: "json",
      success: function (data) {
        alert('');
      }
    });
  }