2014-01-22 270 views
0

我一直在爲這一段時間打破了我的頭。我有下面的表單,工作正常。MVC jQuery刪除操作不起作用

@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> 
} 

這裏的問題在於刪除。它調用該函數,但不能正確執行。它不打我的控制器動作,但與其他控制器動作嘗試它,它的工作:

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

參數事項的名稱。你應該根據行動宣言重命名您的請求參數:

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

在你的控制器類,你命名的參數pclLineId而不是id

您可以:

  1. 重命名參數id
  2. 改變你的jQuery函數傳遞的pclLineId代替id
0

//更改功能

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