2016-06-13 1 views
0

그래서 "링크"개체를 저장하는 나머지 API가 있습니다. 이제 나머지 API는 "link"객체를 한 번에 하나씩 추가하여 모델에 잘 바인딩하지만, "link"객체의 배열이 전달 될 때 문자열 목록을 바인딩하지 않습니다. 나는 List of Link와 Link []를 가지고있는 모델을 사용하려고 시도했다.모델 내부의 배열이 모델이 배열의 일부일 때 바인딩되지 않습니다.

모델 컨트롤러이

public class Link 
{ 
    [JsonProperty(PropertyName = "name")] 
    public string name { get; set; } 

    [JsonProperty(PropertyName = "fields")] 
    public List<string> fields { get; set; } 

    [JsonProperty(PropertyName = "imgPath")] 
    public string imgPath { get; set; } 

    [JsonProperty(PropertyName = "category")] 
    public string category { get; set; } 
} 

함수 문자열 포맷 보이는 바와 같이, 선단이

$(".category").each(function() 
    { 
     var tag = $(this).attr("id"); 
     var category = $(this).find(".issueTitle").children("span").text(); 
     $("#" + tag + " button").each(function() 
     { 
      var imgPath = $(this).children("i").attr("class"); 
      var fields = $(this).attr("name").split(","); 
      var name = $(this).children("p").children("span").text(); 
      links[i] = { 
       name: name, 
       fields: fields, 
       imgPath: imgPath, 
       category: category 
      }; 
      i++; 
     }); 

    }); 
    $.ajax(
    { 

     url: "/api/Link", 
     type: "Post", 
     async: false, 
     contentType:"application/json", 
     dataType: "json", 
     data: links, 
     success: function() { populate(); }, 
     error: function() { alert("failed to populate"); } 
    }); 

HES 어떤 모양

public string PostLinkList([FromBody] List<Link> links) 
    { 


     string json; 


     bool waiting = true; 
     int timeWaiting = 0; 
     do 
     { 
      try 
      { 
       using (StreamWriter outfile = new StreamWriter("App_Data/linkObjs.txt", false)) 
       { 
        for (int i = 0; i < links.Length; i++) 
        { 
         json = JsonConvert.SerializeObject(links[i]); 
         outfile.Write(json + "\n"); 
        } 

       } 
       waiting = false; 
      } 
      catch (Exception e) 
      { 
       timeWaiting++; 
       if (timeWaiting > 20) 
       { 
        waiting = false; 
       } 
      } 

     } while (waiting); 
     return JsonConvert.SerializeObject(links); 
    } 

이다 like/이것은 백엔드에 전달 된 문자열입니다.

"[ 
{\"name\":\"test name\", 
\"fields\":[\"field\",\"details\"], 
\"imgPath\":\"optImg icon-print\", 
\"category\":\"test\"}, 
{\"name\":\"test name\", 
\"fields\":[\"field\",\"details\"], 
\"imgPath\":\"optImg icon-print\", 
\"category\":\"test\"}, 
{\"name\":\"test name\", 
\"fields\":[\"field\",\"details\"], 
\"imgPath\":\"optImg icon-print\", 
\"category\":\"test\"} 
]" 
+0

json 형식을 게시 할 수 있습니까? – riteshmeher

+0

확실한 분 – ZergRush

+1

을 사용해 봤습니까? JSON.stringify (링크) – riteshmeher

답변

0

json.stringify을 사용하여 jquery Ajax 호출을 할 때 json 객체를 직렬화해야합니다.

0

오케이 그래서 고칠 수있는 걸 발견했습니다. 업데이트 된 코드가 실행 중이 아니기 때문에 새 가상 디렉터리를 만들어야했습니다. JSON.string이 링크를 보내서 백엔드에 대한 링크를 보내지 않은 경우 [] 목록을 수정하지 않고 모든 문제를 해결했습니다. 도움을 주신 모든 분들께 감사드립니다. 미안하지만 너 시간 낭비 했어.

관련 문제