【问题标题】:Executing an action by an AJAX request DNN MVC通过 AJAX 请求 DNN MVC 执行操作
【发布时间】:2016-07-02 12:30:24
【问题描述】:

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

我想要达到的目标

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

JSON REPONSE

我的控制器

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

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

我的观点

下面的视图不是我的最终视图,而是用来测试的。

$("#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

【问题讨论】:

标签: jquery json asp.net-mvc dotnetnuke


【解决方案1】:

根据 DNN 在 Unsupported MVC Features 上的文档,目前不支持 ActionResult 以外的任何结果。

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

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

但实际上,在 DNN 添加对其他 ActionResult 类型的支持之前,您需要为这些类型的服务创建 WebAPI 服务或处理程序。

【讨论】:

【解决方案2】:

您需要像下面这样传递 url 和标题:

$.ajax({

type: 'post',

url: "/DesktopModules/MVC/YourModuleName/controllerName/actionName",

data: {itemId:JSON.stringify(16)},

headers: {

"ModuleId": @Dnn.ModuleContext.ModuleId,

"TabId":@Dnn.ModuleContext.TabId,

"RequestVerificationToken": $("input[name='__RequestVerificationToken']").val()

},

success: function (data) { ... },

error: function () { ... }

});

================================================ ========

并且您必须在 Module 的根目录中具有 RouteConfig.cs 文件,其中包含以下内容才能注册路由:

using DotNetNuke.Web.Mvc.Routing;
namespace Dnn.Modules.YourModuleName

{

public class RouteConfig : IMvcRouteMapper

{

public void RegisterRoutes(IMapRoute mapRouteManager)

{

mapRouteManager.MapRoute("YourModuleName", "YourModuleName", "{controller}/{action}", new[]

{"Dnn.Modules.YourModuleName.Controllers"});

}

}

}

===========================================

但是!!!

在 dnn mvc 模块中使用 ajax 的最佳方式是使用 DnnMvcAjaxHandler

那么你可以拥有:

@AjaxHandler.ActionLink(linkText: "delete", actionName: "delete", controllerName: "home", routeValues: new { id = item.Id }, ajaxOption:

new AjaxHandlerOption

{

LoadingElementId = "#global-ajax-loading",

OnConfirm = "are you sure ?",

},

htmlAttributes: new { @class = "btn btn-danger btn-xs" })

【讨论】:

    【解决方案3】:

    我也遇到了困难,在尝试了我在网上找到的所有内容后,最终解决了这个问题的方法是替换 javascript 中的 url:

    @Url.Action("ActionName", "ControllerName", new {SomeParameter = "SomeValue"))​ 致:

    dnn.getVar("sf_siteRoot", "/") + "DesktopModules/MVC/ModuleName/Controller/Action"​

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 2012-02-07
      相关资源
      最近更新 更多