通过AJAX请求DNN MVC执行操作 [英] Executing an action by an AJAX request DNN MVC

查看:153
本文介绍了通过AJAX请求DNN MVC执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的DNN MVC开发过程中出现了另一个问题.我想知道这是否是我犯了一个错误的错误/缺失功能. 我将尝试在下面解释问题.

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.

我想实现的目标

我想通过调用AJAX帖子在控制器中执行操作.到目前为止,它仍然有效. 但是,当我尝试将一些变量返回到View时,它不仅返回变量.它将整个HTML源添加到响应中.

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响应

我的控制器

我的方法返回一个ActionResult(我尝试了JsonResult和其他东西:HttpResponseMessage). 该程序本身正在运行,并且确实返回JSON消息.

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

我的视图

下面的视图不是我的最终视图,而是要进行测试.

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

这是错误还是我可以解决的问题?非常感谢您的宝贵时间. https://dnntracker.atlassian.net/browse/DNN-8522

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

推荐答案

根据DNN在

According to DNN's documentation on Unsupported MVC Features, any result other than ActionResult isn't currently supported.

我能够在我的MVC控制器方法之一中返回JsonResult,它将返回JSON响应,但是正如您提到的,附加到它的是View HTML.我的解决方案是解析响应以从中获取json字符串.

I was able to return a JsonResult in one of my MVC controller methods and it would return the JSON response but, like you mentioned, appended to it was the View HTML. My hacky solution was to parse the response to get my json string out of it.

...
success: function (response) {
    var dnnViewResp = response.xhr.responseText;
    dnnViewResp = dnnViewResp.substring(0, dnnViewResp.indexOf("<!DOCTYPE html>"));
    var data = JSON.parse(dnnViewResp);
}

但是,实际上,您需要为这些类型的服务创建WebAPI服务或处理程序,直到DNN添加对其他ActionResult类型的支持为止.

But really, you need to create a WebAPI service or handler for these types of services until DNN adds support for other ActionResult types.

这篇关于通过AJAX请求DNN MVC执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆