C# Asp.net MVC 4 Ajax post请求不起作用,我可以';我不明白为什么

C# Asp.net MVC 4 Ajax post请求不起作用,我可以';我不明白为什么,c#,javascript,asp.net,asp.net-mvc-4,jquery,C#,Javascript,Asp.net,Asp.net Mvc 4,Jquery,我正在开发一个ASP.NETMVC4应用程序,在其中一个页面中,我有一个表单,我正在迭代所有输入并转换为json数据 我使用fiddler、chrome开发工具和firebug验证了数据是否正确地转换为json。我使用$ajax将数据发布到mvc控制器,每次都会收到一个错误 我似乎不明白为什么它不起作用。我打赌如果我能弄清楚如何得到错误信息,我就能弄清楚如何让它工作。当我尝试在控制台中查看时,我没有看到错误消息。我尝试将错误消息登录到控制台,但没有显示任何内容 如果有人能帮我找出如何得到错误信息

我正在开发一个ASP.NETMVC4应用程序,在其中一个页面中,我有一个表单,我正在迭代所有输入并转换为json数据

我使用fiddler、chrome开发工具和firebug验证了数据是否正确地转换为json。我使用$ajax将数据发布到mvc控制器,每次都会收到一个错误

我似乎不明白为什么它不起作用。我打赌如果我能弄清楚如何得到错误信息,我就能弄清楚如何让它工作。当我尝试在控制台中查看时,我没有看到错误消息。我尝试将错误消息登录到控制台,但没有显示任何内容

如果有人能帮我找出如何得到错误信息,我将不胜感激。我还发布了代码,以防我遗漏了一些显而易见的东西

以下是我的javascript代码:

$(function () {

$("#saveform").submit(function () {
    var myRows = [];
    var $headers = $("th");
    var $rows = $("tbody tr").each(function (i, n) {
        var $row = $(n);
        myRows.push({
            id: $row.find('#item_SpecimenId').val(),
            site: $row.find('#item_Site').val(),
            clinicalImpression: $row.find('#item_ClinicalImpression').val(),
            degreeOfSuspicion: $row.find('#item_DegreeOfSuspicion').val(),
            margins: $row.find('#item_Margins :selected').text(),
            count: $row.find('#item_Count').val(),
            lesionSize: $row.find('#item_LesionSize').val(),
        });

    });
    var specimens = JSON.stringify(myRows);
    var url = '/PathSlip/DoctorSlip';
    $.ajax({
        url: url,
        data: '{ id:1, post:2 }',
        type: 'POST',
        dataType: 'json',
        statusCode: {
            404: function () {
                $("#response").html('Could not contact server.');
            },
            500: function () {
                $("#response").html('A server-side error has occurred.');
            }
        },
        success: function () {
            $("#response").html('success');
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $("#response").html('an error');
        }
    });

});
});
这是我在控制器中使用的viewModel

public class SpecimenViewModel
{

    public int id { get; set; }
    public string site { get; set; }
    public string clinicalImpression { get; set; }
    public string degreeOfSuspicion { get; set; }
    public string margins { get; set; }
    public string count { get; set; }
    public string lesionSize { get; set; }
}
    public ActionResult DoctorSlip(int id)
    {
        var specimens = _repo.GetSpecimensByBiopsyId(id).ToList();
        return View(specimens);
    }


    [HttpPost]
    public ActionResult DoctorSlip(IEnumerable<SpecimenViewModel> specimens)
    {
        return RedirectToAction("DoctorSlip");
    }
这是我的mvc控制器

public class SpecimenViewModel
{

    public int id { get; set; }
    public string site { get; set; }
    public string clinicalImpression { get; set; }
    public string degreeOfSuspicion { get; set; }
    public string margins { get; set; }
    public string count { get; set; }
    public string lesionSize { get; set; }
}
    public ActionResult DoctorSlip(int id)
    {
        var specimens = _repo.GetSpecimensByBiopsyId(id).ToList();
        return View(specimens);
    }


    [HttpPost]
    public ActionResult DoctorSlip(IEnumerable<SpecimenViewModel> specimens)
    {
        return RedirectToAction("DoctorSlip");
    }
public ActionResult DoctorSlip(int-id)
{
var样本=_repo.GetSpecimensByBiopsyId(id.ToList();
返回视图(样本);
}
[HttpPost]
公共行动结果博士(IEnumerable样本)
{
返回重定向到操作(“DoctorSlip”);
}

非常感谢您的帮助。

我认为您的代码中存在一个问题:

您在“数据”中传递单个字段,即“id,post”,而您的操作方法需要
SpecimenViewModel
对象列表。您应该传递
样本
对象

首先,尝试发送正确的数据,如果问题仍然存在,只需将returnofpost方法更改为
returnthis.View()而不是
返回重定向到操作(“DoctorSlip”)

我准备了与您的代码稍有不同的版本,您可以尝试:

@using (Html.BeginForm("DoctorSlip","Test",FormMethod.Post,new {id="saveform"}))
{
    <!-- your other input controls -->
    <input type="button" id="submitForm" value="Save"/>
}
@使用(Html.BeginForm(“DoctorSlip”,“Test”,FormMethod.Post,new{id=“saveform”}))
{
}
脚本:

<script>
    $(function() {

        $("#submitForm").click(function () {
            var myRows = [];
                myRows.push({
                    id: 1,
                    site: "abc",
                    clinicalImpression: "aa",
                    degreeOfSuspicion: "test",
                    margins: "24",
                    count: "12",
                    lesionSize: "33",
                });

                myRows.push({
                    id: 2,
                    site: "abc2",
                    clinicalImpression: "aa3",
                    degreeOfSuspicion: "test2",
                    margins: "24",
                    count: "12",
                    lesionSize: "33",
                });

            var specimens = JSON.stringify(myRows);
            var url = '/Test/DoctorSlip';
            $.ajax({
                url: url,
                data: specimens,
                type: 'POST',
                dataType: 'json',
                statusCode: {
                    404: function() {
                        alert('Could not contact server.');
                    },
                    500: function() {
                        alert('A server-side error has occurred.');
                    }
                },
                success: function() {
                    alert('success');
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert('an error' + thrownError);
                }
            });

        });
    });
</script>

$(函数(){
$(“#提交表单”)。单击(函数(){
var myRows=[];
myRows.push({
id:1,
网站:“abc”,
临床表现:“aa”,
怀疑程度:“测试”,
页边空白:“24”,
计数:“12”,
lesionSize:“33”,
});
myRows.push({
id:2,
网站:“abc2”,
临床表现:“aa3”,
怀疑程度:“测试2”,
页边空白:“24”,
计数:“12”,
lesionSize:“33”,
});
var samples=JSON.stringify(myRows);
var url='/Test/DoctorSlip';
$.ajax({
url:url,
资料:标本,
键入:“POST”,
数据类型:“json”,
状态代码:{
404:函数(){
警报('无法联系服务器');
},
500:函数(){
警报('发生服务器端错误');
}
},
成功:函数(){
警惕(“成功”);
},
错误:函数(xhr、ajaxOptions、thrownError){
警报(“错误”+thrownError);
}
});
});
});

当我使用
$.post
而不是
$.ajax
时,它起了作用,但我不知道为什么。

真是一团糟。为什么不让Javascript函数调用独立函数而不是嵌套函数?然后你可以一个接一个地测试它们,你的代码会更干净。你定义了一个数组,但在你的数据对象中你只是传递了几个数字。你在看什么,你期望看到什么?你没有经过你的弦化物体。。它期待一份清单。。但它只发布了一个对象。你试过chrome inspector中的“网络”选项卡吗?对不起,我尝试了很多不同的方法来解决这个问题。我传递的样本列表不起作用,所以我将其更改为1个对象,以确保它不是我传递的数据。在chrome inspector networking选项卡中,除了加载我的javascript、css和html页面之外,我还看到一个302找到状态代码,用于向我的控制器发送帖子,然后加载页面。