2012-04-06 13 views
5

मैं निम्नलिखित जावास्क्रिप्ट वर्ग जो मैं एक ASP.NET MVC नियंत्रकASP.NET MVC के लिए जावास्क्रिप्ट सरणी पासिंग नियंत्रक

var postParams = { 

     attributes : [ 
         { 
          name : '', 
          description: '', 
          attributeType : '', 
          value : '' 
         } 
         ] 
    }; 

postParams.attributes[0] = new Object(); 
postParams.attributes[0].name = "test"; 
postParams.attributes[0].description = "test"; 
postParams.attributes[0].attributeType = "test"; 
postParams.attributes[0].value = "test"; 

को पारित करने के लिए कोशिश कर रहा हूँ यहाँ कैसे मैं नियंत्रक विधि कॉल है है:

var actionURL = "@Url.Action("Save", "Catalog")"; 
    $.ajax({ 
        type: "POST", 
        url: actionURL, 
        data: postParams ...... 

public class AttributeViewModel 
{ 
    public string name { get; set; } 
    public string description { get; set; } 
    public string attributeType { get; set; } 
    public string value { get; set; } 
} 

मेरे नियंत्रक सहेजें विधि डे है:

नियंत्रक तरफ मैं एक ViewModel इस प्रकार के रूप में घोषित किया है clared इस प्रकार है:

public JsonResult Save(AttributeViewModel[] attributes) 

जब मैं निष्पादित विशेषताओं के मूल्य हमेशा रिक्त है।

कोई विचार? यह सुनिश्चित नहीं है कि इसे डिबगिंग कैसे शुरू करें।

उत्तर

6

आप अपनी समस्या

[JsonFilter(Param = "attributes", JsonDataType = typeof(AttributeViewModel[]))] 
    public JsonResult Save(AttributeViewModel[] attributes) 

हल करने के लिए ग्राहक पर json.net पुस्तकालय की कोशिश कर सकते हैं:

$.ajax({ 
     type: 'POST', 
     url: url, 
     async: true, 
     data: JSON.stringify(attributes), //!!! Here must be the same name as in controller method 
     dataType: 'json', 
     contentType: 'application/json; charset=utf-8', 
     success: function (data) { 

     }, 
     error: function (xhr, ajaxOptions, thrownError) { 

     } 
    }); 
+0

सुझाव के लिए धन्यवाद। Json.net के लिए एक संदर्भ जोड़ा गया और फ़िल्टर जोड़ा लेकिन शून्य अभी भी विशेषताओं को पारित कर दिया गया है। – Lance

+0

@ लांस उत्तर क्लाइंट कोड –

+0

के साथ अपडेट किया गया धन्यवाद धन्यवाद, JSON.stringify() ने एक इलाज किया !! – Lance

1

लगता है आप की तरह ajax कॉल करने के लिए traditional : true जोड़ने की जरूरत है। here और here

$.ajax({ 
       traditional: true, 
       type: "POST", 
       url: actionURL, 
       data: postParams 
+0

पारंपरिक में जोड़ा गया: सच लेकिन यह समस्या का समाधान नहीं किया। मैं इस पर बहुत लंबे समय से रहा हूं इसलिए मैं अब के लिए एक अल्पविराम से अलग सरणी में नियंत्रक को पास कर दूंगा। – Lance

संबंधित मुद्दे