0

HTML

 <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 Controller Method

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});
    }

doc is always null, what am I missing. Please advice. Thanks

2
  • 1
    Just var jsonData = { doc: $(this).attr('href') }; (no quotes) Commented Jul 21, 2016 at 5:19
  • @StephenMuecke . great quick solution. Thanksss! Commented Jul 21, 2016 at 5:30

1 Answer 1

1

The problem is in your json constrruction you have to construct like this.

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

as you are sending a post request to an action it's better to add [HttpPost] attribute on actions.

    [HttpPost]
    public JsonResult Delete(string doc)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.