C# 在C MVC中使用Jquery Ajax Post方法时总是出现错误

C# 在C MVC中使用Jquery Ajax Post方法时总是出现错误,c#,jquery,asp.net-mvc,http-post,C#,Jquery,Asp.net Mvc,Http Post,我试图在我的视图中使用ajax来更新用户配置文件中的更改。不幸的是,无论我尝试什么,我都会收到错误消息error!!!。我认为问题出在我的模型上,我已经尝试过对它进行定制,但它仍然不起作用 以下是用户按下保存按钮时执行的JS: $(document.getElementById('save')).on("click", function() { var liSportMotives = $('#sortable li').map(function(i, n) {

我试图在我的视图中使用ajax来更新用户配置文件中的更改。不幸的是,无论我尝试什么,我都会收到错误消息error!!!。我认为问题出在我的模型上,我已经尝试过对它进行定制,但它仍然不起作用

以下是用户按下保存按钮时执行的JS:

$(document.getElementById('save')).on("click", function() {
            var liSportMotives = $('#sortable li').map(function(i, n) {
                return $(n).attr('id');
            }).get();
            var liActivities = $('#myCarousel div').map(function(i, n) {
                if ($(n).hasClass('thumbnailActive')) {
                    return $(n).attr('id');
                }
            }).get();
            var myPostJSONObject = {
            Weight : parseFloat($(document.getElementById('weightrange')).val()).toFixed(1),
            Height : parseFloat($(document.getElementById('heightrange')).val()).toFixed(2),
            IsSportsBeginnerPractise : $("input:radio[name='IsSportsBeginnerPractise']:checked").val(),
            IsSportsBeginnerKnowledge : $("input:radio[name='IsSportsBeginnerKnowledge']:checked").val(),
            IsNutritionBeginnerPractise : $("input:radio[name='IsNutritionBeginnerPractise']:checked").val(),
            IsNutritionBeginnerKnowledge : $("input:radio[name='IsNutritionBeginnerKnowledge']:checked").val(),
            NewSportMotives : liSportMotives,
            NewActivities : liActivities
        };
            $.ajax({
                url: '@Url.Action("ManageConfirmation", "Account")',
                type: "POST",
                traditional: true,
                data: JSON.stringify(myPostJSONObject),
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    alert("Success!");
                },
                error: function () {
                    alert("Error!!!");
                }
            });
        });
控制器中的模型如下所示:

public class ManageUserDetails
        {
            public decimal Height { get; set; }

            public decimal Weight { get; set; }

            public bool IsSportsBeginnerPractise { get; set; }

            public bool IsSportsBeginnerKnowledge { get; set; }

            public bool IsNutritionBeginnerPractise { get; set; }

            public bool IsNutritionBeginnerKnowledge { get; set; }

            public String[] NewSportMotives { get; set; }

            public String[] NewActivities { get; set; }
        }
    public ActionResult ManageConfirmation(ManageUserDetails model)
    {
        return RedirectToAction("Index", "Home");
    }
我有一个动作是这样的:

public class ManageUserDetails
        {
            public decimal Height { get; set; }

            public decimal Weight { get; set; }

            public bool IsSportsBeginnerPractise { get; set; }

            public bool IsSportsBeginnerKnowledge { get; set; }

            public bool IsNutritionBeginnerPractise { get; set; }

            public bool IsNutritionBeginnerKnowledge { get; set; }

            public String[] NewSportMotives { get; set; }

            public String[] NewActivities { get; set; }
        }
    public ActionResult ManageConfirmation(ManageUserDetails model)
    {
        return RedirectToAction("Index", "Home");
    }
由于返回RedirectToActionIndex,Home;,您的操作正在尝试重定向

这将导致一些未实施的问题

您可以尝试以下方法:

[HttpPost]
public JsonResult ManageConfirmation(ManageUserDetails model)
{
    //do stuff e.g. save model.
    return Json(new { data = "some data to send back" }, JsonRequestBehavior.AllowGet);
}

在Ajax调用中不应重定向到任何。 相反,请使用以下命令:

public ActionResult ManageConfirmation(ManageUserDetails model)
    {
        return Json(new { data = "true" }, JsonRequestBehavior.AllowGet);
    }
在Ajax调用中:

$.ajax({
                url: '@Url.Action("ManageConfirmation", "Account")',
                type: "POST",
                traditional: true,
                data: JSON.stringify(myPostJSONObject),
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                window.location.href = '@Url.Action("Index","Home")';
                    alert("Success!");
                },
                error: function () {
                    alert("Error!!!");
                }
            });

查看成功方法。

您是否使用[HttpPost]属性标记您的操作?另外-至少在错误方法中添加console.logarguments,以便您可以准确查看浏览器控制台中的错误。@FuchurKool:您好,感谢通常通过向上投票表示-