Javascript 将JSON传递给控制器操作问题

Javascript 将JSON传递给控制器操作问题,javascript,asp.net,asp.net-mvc,json,Javascript,Asp.net,Asp.net Mvc,Json,我有以下javascript 问题是,如果我在“配料”表中输入了一行,但在对我的C#对象进行序列化之后,我在最终传递给控制器的操作中得到了两行。但是第二个对象是空的 我检查了javascript,变量“cnt”是1而不是2 为什么会这样 马尔科姆 [守则] $("#Save").click(function () { var title = $("#recipetitle").val(); var category = $("#category").val(); var preptime = $

我有以下javascript

问题是,如果我在“配料”表中输入了一行,但在对我的C#对象进行序列化之后,我在最终传递给控制器的操作中得到了两行。但是第二个对象是空的

我检查了javascript,变量“cnt”是1而不是2

为什么会这样

马尔科姆

[守则]

$("#Save").click(function () {
var title = $("#recipetitle").val();
var category = $("#category").val();
var preptime = $("#prepTime").val();
var preptimeperiod = $("#lstPrepTime").val();
var cooktime = $("#cookTime").val();
var cooktimeperiod = $("#lstCookTime").val();
var rating = $("#rating").val();
var method = $("#method").val();

var jsontext = '{ "RecipeTitle": "' + title + '",';
jsontext += '"CategoryID":' + category + ',';
jsontext += '"PrepTime":' + preptime + ',';
jsontext += '"PrepTimePeriod":"' + preptimeperiod + '",';
jsontext += '"CookTime":' + cooktime + ',';
jsontext += '"CookTimePeriod":"' + cooktimeperiod + '",';
jsontext += '"Rating":' + rating + ',';
jsontext += '"Method":"' + method + '",';

var ing = "";
var cnt = 0;
$("#ingredients tr.ingredientdata").each(function () {
    if ($("td.ingredient", this).text() != "") {
        ing += '{ "IngredientName": "' + $("td.ingredient", this).text() + '",';
        ing += '"Units": ' + $("td.units", this).text() + ',';
        ing += '"Measure": "' + $("td.measure", this).text() + '"} ,';
    }
    cnt = cnt + 1;
});
alert(cnt);
if (ing != "") {
    jsontext += '"Ingredients": [';
    ing = ing.substring(0, jsontext.length - 1);
    jsontext = jsontext + ing;
    jsontext += ']';
}
jsontext += '}';

var json = eval('(' + jsontext + ')');
//var json = { Field: 1 };

$.ajax({
    url: "/Recipe/Save",
    type: "POST",
    dataType: 'json',
    data: JSON.stringify(json),
    contentType: "application/json; charset=utf-8",
    success: function () {
        //alert("DONE!!");
    }
});
}));
[/code]

我建议对javascript进行重构,因为它可以帮助您更容易地识别错误。同时使用FireBug签出发送给控制器的实际JSON请求:

$("#Save").click(function () {
    var ingredients = $('#ingredients tr.ingredientdata').map(function(index, element) {
        return {
            ingredientName: $('td.ingredient', element).text(),
            units: $('td.units', element).text(),
            measure: $('td.measure', element).text()
        };
    });

    var json = {
        RecipeTitle: $('#recipetitle').val(),
        CategoryID: $('#category').val(),
        PrepTime: $('#prepTime').val(),
        PrepTimePeriod: $('#lstPrepTime').val(),
        CookTime: $('#cookTime').val(),
        CookTimePeriod: $('#lstCookTime').val(),
        Rating: $('#rating').val(),
        Method: $('#method').val(),
        Ingredients: ingredients
    };

    $.ajax({
        url: '/Recipe/Save',
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(json),
        contentType: 'application/json; charset=utf-8',
        success: function () {
            //alert("DONE!!");
        }
    });
});