2016-11-22 14 views
1

私はAJAXを使用してドロップダウンリストの値をコントローラーにポストする機能を持っています。コントローラーは、選択した値に依存するデータを返します。MVC AjaxコントローラーへのPOSTデータは常にnull

問題は、私は値として掲載取得された開発者ツールで見ることができる、それは常にされている何も返さないフォームデータ{"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

を動作するかどうか試してみてくださいあなたはJavaScriptをデバッグする場合、propertyType'が設定 'のですか?また、データオブジェクトの周りにJSON.stringify()を置く必要はないと思っています。単に 'data:{PropertyType:propertyType} 'を実行します。 – Luke

+1

' dataType:' json''と 'contentType : "application/json; charset = utf-8"、もう一度やり直してください。 – Win

+0

問題を引き起こしたのはJSON.stringify()でした...ありがとうございます。 – JBoom

答えて

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); 
      } 

     }); 
    } 
関連する問題