MVC Ajax POST数据到控制器始终为null

时间:2016-11-22 16:06:39

标签: jquery ajax asp.net-mvc

我有一个函数,它使用AJAX将下拉列表的值发布到控制器,然后控制器返回依赖于所选值的数据。

问题是不断返回任何内容,我可以在开发人员工具中看到该值已作为表单数据发布{"PropertyType":"House"}

当控制器被触发时,调试PropertyType一直是null,但似乎看不出原因。

AJAX

function PropertyStyleFilter() {

    var propertyType = $('#PropertyType').val();

    var Url = '@Url.Action("PropertyStyleFilter")';
    //var Url = '/Case/CaseDetails/PropertyStyleFilter/' + PropertyType;
    console.log("Property Type:" + PropertyType);

    $.ajax({
        url: Url,
        data: JSON.stringify({ PropertyType: propertyType }),
        type: 'POST',
        success: function (data) {
            $("#PropertyStyle").html(""); // clear before appending new list

            $("#PropertyStyle").append($('<option></option>').val("").html("Select Property Style..."));
            $.each(data, function (i, style) {
                //console.log(i, site);
                $("#PropertyStyle").append($('<option></option>').val(style.Value).html(style.Text));
            });
            if (PropertyType != null) {
                $("#PropertyStyle").val(PropertyType);
            }
        },
        error: function (__x, __h, __m) {
            console.log('ajax returned error', __m, __x, __h);
        }

    });
}

控制器

[HttpPost]
public ActionResult PropertyStyleFilter(string PropertyType)
{
    var StyleList = (from ps in efContext.PropertyStyles
                    join pt in efContext.PropertyTypes
                    on ps.PropertyTypeId equals pt.Id
                    where pt.TypeName == PropertyType
                        orderby ps.Id
                    select new SelectListItem
                    {
                        Value = ps.StyleName,
                        Text = ps.StyleName
                    });


    return Json(StyleList, JsonRequestBehavior.AllowGet);

}

1 个答案:

答案 0 :(得分:0)

尝试这是否有效

function PropertyStyleFilter() {

        var propertyType = $('#PropertyType').val();

        var Url = '@Url.Action("PropertyStyleFilter")';
        //var Url = '/Case/CaseDetails/PropertyStyleFilter/' + PropertyType;
        console.log("Property Type:" + PropertyType);

        $.ajax({
            url: Url,
    dataType: 'json',
            data: { PropertyType: propertyType },
            type: 'POST',
            success: function (data) {
                $("#PropertyStyle").html(""); // clear before appending new list

                $("#PropertyStyle").append($('<option></option>').val("").html("Select Property Style..."));
                $.each(data, function (i, style) {
                    //console.log(i, site);
                    $("#PropertyStyle").append($('<option></option>').val(style.Value).html(style.Text));
                });
                if (PropertyType != null) {
                    $("#PropertyStyle").val(PropertyType);
                }
            },
            error: function (__x, __h, __m) {
                console.log('ajax returned error', __m, __x, __h);
            }

        });
    }