2016-07-21 89 views
0

HTMLMVC JsonResult控制器參數空

<a href="@item.documentId" class="glyphicon glyphicon-remove-circle del-file"></a> 

的JavaScript

$(".del-file").click(function() { 
     alert($(this).attr('href')); 
     var jsonData = "{'doc':'" + $(this).attr('href') + "'}"; 

     var parent_row = $(this).closest('tr'); 

     $.post('@Url.Action("Delete","Documents")', jsonData) 
      .success(function (response) { 
       if (response.result == true) { 
        $(parent_row).remove(); 
       } 
      }) 
      .error(function (jqXHR, textStatus, errorThrown) { 
       if (jqXHR.status == 500) { 
        alert('Internal error: ' + jqXHR.responseText); 
       } else { 
        alert('Unexpected error.'); 
       } 
      }) 
     return false; 
    }) 

MVC控制器的方法

public JsonResult Delete(string doc) 
    {    
     long docId = Helpers.Utility.Instance.getIdAfterDecode(doc);    
     if (docId <=0) 
     { 
      return Json(new { result = "error: Document info was not correct." }); 
     } 
     bool output = new DocumentsInfoRepository().deleteDocument(docId); 
     return Json(new { result = output}); 
    } 

文檔是ALWA ys null,我錯過了什麼。請指教。謝謝

+1

只是'VAR jsonData = {DOC:$(本).attr( 'href' 屬性)};'(不含引號) –

+0

@StephenMuecke。很快的解決方案。 Thanksss! – Sami

回答

1

問題是在你的json構造你必須像這樣構造。

var jsonData = {doc:$(this).attr('href')}; 

爲您發送POST請求最好是在動作添加[HttpPost]屬性的作用。

​​18