Knockout asyncCommand / Ajax MVC ActionResults

Knockout asyncCommand / Ajax MVC ActionResults

本文关键字:MVC ActionResults Ajax asyncCommand Knockout      更新时间:2023-09-26

我对前端开发和我正在进行的项目(错误修复)都很陌生。我正在使用Knockout/MVC ActionResults。

我的任务是修改一个表单,使其不允许双击提交,我添加了基本的Jquery来禁用按钮,但被告知它需要使用asyncCommand。

然而,我注意到的是,所有使用它的例子都会返回Json结果等,而我的结果将是操作结果。

我想知道我是否需要更改MVC操作结果,或者是否还有其他我看不到的方法。

我希望我能做一些类似的事情:

HTML

<button class="btn btn-action" data-bind="command: create, activity: create.isExecuting">Create</button> 

JS-

 model.create = ko.asyncCommand({
        execute: function (complete) {
            $.ajax({
                type: 'POST',
                url: "/account/create",
                data: ko.toJSON(data),
                contentType: 'application/json; charset=utf-8',
            });
        complete();
        },
        canExecute: function (isExecuting) {
            return !isExecuting;
        }
    });

控制器

        public ActionResult Create(Model model, int versionId)
        {
if (success)
                {
                    return Redirect("/accounts");
                }
else
                {
                    return View(model);
                }
}

这真的会发生吗?

谢谢,

Clare

我的解决方案是更改ActionResult以向AJAX返回值。