为.val()获取空

时间:2014-02-12 15:00:17

标签: c# jquery ajax asp.net-mvc

我有输入和选择。出于某种原因,他们中的一些人给了我一个空的。这是代码:

<script type="text/javascript">
$(function () {
    $('#add').on('click', function () {
        $('table').append('<tr>' +
                '<td><button class=\'save\'>Save</button></td>' +
                '<td><input name=\'name\' id=\'companyName\' /></td>' +
                '<td><input name=\'currency\' id=\'currency\' /></td>' +
                '<td><input name=\'ISOCode\' id=\'ISOCode\' /></td>' +
                '<td><input name=\'CalcDeadLine\' id=\'CalcDeadLine\' /></td>' +
                '<td><select name=\'mealAlgorithm\' id=\'mealAlgorithm\'><option value="True">Yes</option><option value="False">No</option></select></td>' +
                '<td><input name=\'breakfast\' id=\'breakfast\' /></td>' +
                '<td><input name=\'halfBoard\' id=\'halfBoard\' /></td>' +
                '<td><input name=\'fullBoard\' id=\'fullBoard\' /></td>' +
                '<td><input name=\'adminID\' id=\'adminID\' /></td>' +
                '<td><input name=\'language\' id=\'language\' /></td>' +
                '<td><input name=\'approvalcid\' id=\'cid\' /></td>' +
                '<td><select name=\'useSMS\' id=\'useSMS\'><option value="True">Yes</option><option value="False">No</option></select></td>' +
                '<td><select name=\'active\' id=\'active\'><option value="True">Yes</option><option value="False">No</option></select></td>');

        $('#add').hide();
    })
});

$(".save").live("click", function () {
    var name = $("#companyName").val();
    var currency = $("#currency").val();
    var isoCode = $("#ISOCode").val();
    var calcDeadLine = $("#CalcDeadLine").val();
    var mealAlgorithm = $("#mealAlgorithm").val();
    var noMeal = 111;
    var breakfast = $("#breakfast").val();
    var halfBoard = $("#halfBoard").val();
    var fullBoard = $("#fullBoard").val();
    var adminID = $("#adminID").val();
    var language = $("#language").val();
    var cid = $("#cid").val();
    var useSms = $("#useSMS").val();
    var active = $("#active").val();

    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        url: '@Url.Action("SaveCompany", "Admin")',
        data: {
            "CompanyName": name, "Currency": currency, "ISOCompanyCode": isoCode, "CalcDeadline": calcDeadLine,
            "UseMealAlgorithm": mealAlgorithm, "NoMeal": noMeal, "Breakfast": breakfast, "HalfBoard": halfBoard,
            "FullBoard": fullBoard, "AdminUserID": adminID, "ApprovalCulture": language, "ApprovalLcid": cid,
            "UseSMS": useSms, "Active": active},
        dataType: "json",
        beforeSend: function () {
        },
        success: function (data) {
            if (data.result == true) {
                $("#GridCompany").html("Record has been saved!");
            }
            else {
                alert("There is some error.");
            }
        }
    })
})

在帖子的网址中一切正常,它有真/假,每个参数都满足每个值。

在我的控制器中我有

[HttpGet]
    public JsonResult SaveCompany(string name, string currency, string isoCode, int calcDeadline, bool? mealAlgorithm,
        int noMeal, int breakfast, int halfBoard, int fullBoard, string adminUserId, string approvalCulture,
        int? approvalCid, bool? useSms, bool? active)
    {
        bool result = false;

        try
        {
            result = _companyRepository.SaveCompany(name, currency, isoCode, calcDeadline, mealAlgorithm, noMeal,
                breakfast, halfBoard, fullBoard, adminUserId, approvalCulture, approvalCid, useSms, active);
        }
        catch(Exception ex)
        {
        }

        return Json(new { result }, JsonRequestBehavior.AllowGet);
    }

Null是isoCode,mealAlgorithm,approvalCid。

1 个答案:

答案 0 :(得分:1)

问题是字段名称错误。 在AJAX中,您传递了ISOCompanyCode,但在等待isoCode的行动中 其他两个字段也是一样的(你有不同的名字)。使用相同的名称,它将解决您的问题。