Executing an action by an AJAX request DNN MVC

Viewed 2133

Another problem occured in my DNN MVC development journey. I am wondering whether this is a bug/missing feature of I've made a mistake. I will try to explain the problem below.

What I want to achieve

I want to execute an action in my controller by calling an AJAX post. So far it works. However, when I try to return some variables to my View, it doesn't only return the variables. It adds an whole HTML-source to the response.

JSON REPONSE

My controller

My method returns an ActionResult (I tried JsonResult and something different: HttpResponseMessage as well). The program itself is working and does return the JSON-message.

public ActionResult Mark(int itemId)
{
    var item = _itemService.GetItem(itemId, ModuleContext.ModuleId);

    // Check if item exists
    if (item == null)
        return Json(new { success = false, Description = "the item you want to vote for does not exist (anymore)" }, JsonRequestBehavior.AllowGet);

    // Check if user is voting on his own item
    if (item.CreatedByUserId == User.UserID)
        return Json(new { success = false, Description = "it is not allowed to vote for your own item" },  JsonRequestBehavior.AllowGet);

My View

The View below isn't my final View, but's meant to test.

$("#button").click(function () {
    $.ajax({
     type: "POST",
        url: "@Url.Action("Mark", "Vote")",
        dataType: "json",
        data: { itemId: JSON.stringify(16) },
        success: function (data) { console.log(data); },
        error: function (errMsg) {
            responseText = jQuery.parseJSON(errMsg.responseText);
            console.log(responseText.Description);
        }
    });
});

Is this a bug, or something I am able to fix? Thank you very much for your time. https://dnntracker.atlassian.net/browse/DNN-8522

3 Answers
Related