2016-07-14 3 views
5

Backgournd모델에

나는 현재 모델의 목록에 값을 추가하려고하지만 내가 자바 스크립트에서 그것을 얼마나 다른 것 같다을 기반으로하는 목록에 값을 추가 .

모델

public class ContentBase 
{ 

    public string Name { get; set; }  
    public string Description { get; set; } 
    public string LastModifiedTime { get; set; } 
    public string KeyWords { get; set; } 
    public string Content { get; set; } 
    public string UniqueId { get; set; } 
    public string library { get; set; } 
    public string ContentSummary { get; set; }   
    public string[] images { get; set; } 
} 

기능 값을 추가하려면

List<ContentBase> returnList = new List<ContentBase>(); 
foreach (HtmlNode img in htmlDocument.DocumentNode.SelectNodes("//img")) 
    { 
    HtmlAttribute att = img.Attributes["src"]; 
    returnList.Add(images = att.Value); << this is wrong, just trying to give an idea of my intentions 
    } 

질문 내 리터의 string[] imagesHtmlAttribute att = img.Attributes["src"];에서 결과를 추가하려고

ist. 그런 식으로 한 번 나는

업데이트

이 위의 기능이이를 채 웁니다 내가 내 목록의 string[] images 부분에 각 루프 내에서 얻을 수있는 모든 값을 가질 수있을 것입니다이 반복을 완료 내 목록

string content = ""; 
if (includeContent == true) 
{ 
    content = rslt[ContentBase.fastContent] != null ? rslt[ContentBase.fastContent].ToString() : ""; 
} 

returnList.Add(new ContentBase() 
{ 
    UniqueId = rslt[ContentBase.fasstUniqueId] != null ? rslt[ContentBase.fasstUniqueId].ToString() : "", 
    LastModifiedTime = rslt[ContentBase.fasstLastModifiedTime] != null ? rslt[ContentBase.fasstLastModifiedTime].ToString().Substring(0, 8) : "", 
    Name = rslt[ContentBase.fastName] != null ? rslt[ContentBase.fastName].ToString() : "", 
    Description = rslt[ContentBase.fastDescription] != null ? rslt[ContentBase.fastDescription].ToString() : "", 
    KeyWords = rslt[ContentBase.fastKeyWords] != null ? rslt[ContentBase.fastKeyWords].ToString() : "", 
    ContentSummary = rslt[ContentBase.fasstContentSummary] != null ? rslt[ContentBase.fasstContentSummary].ToString() : "", 
    Content = content, 

}); 
+0

는 –

+0

목록 당신은 그것의'images' 속성을 설정하는 단 하나의'ContentBase' 요소가 포함됩니다 반환 목록의 내부 이미지 속성에 att.Value를 추가하려고? –

+0

자리 비움, 예, 각 루프에 대해 my에서 수신 한 요소가 포함됩니다. 요소 수는 정적이 아닌 동적 일 것입니다. –

답변

0

returnListList<ContentBase>, 그래서 당신이에 새 ContentBase을 추가해야합니다 목록.

ContentBase 개체는 루프에서 만들거나 linq를 사용하여 새로 만든 ContentBase에 전달할 수있는 string[] images 속성을 포함합니다. 또한 생성 된 ContentBase의 다른 속성 값을 제공해야합니다. 예를 들어 :

var returnList = new List<ContentBase>(); 
returnList.Add(new ContentBase() 
{ 
    // Initialize properties the same way you are setting them 
    // Including the images property this way: 
    images = htmlDocument.DocumentNode.SelectNodes("//img") 
         .Select(x=>x.Attributes["src"].Value).ToArray() 
}); 
1
다음

returnList 다른 항목 ContentBase의 목록은 그래서 그 항목이 해당 클래스의 개체를해야합니다. 이 항목을 추가하는 코드처럼 될 것이다 그래서 다음

foreach (HtmlNode img in htmlDocument.DocumentNode.SelectNodes("//img")) 
    { 
    HtmlAttribute att = img.Attributes["src"]; 
    returnList.Add(new ContentBase(){library="some value",images = new string[]{att.value}} 
    } 
1
당신은 그냥 먼저 ContentBase 객체를 생성하고 추가 할 수 있습니다

:

List<ContentBase> returnList = new List<ContentBase>(); 
foreach (HtmlNode img in htmlDocument.DocumentNode.SelectNodes("//img")) 
    { 
    HtmlAttribute att = img.Attributes["src"]; 
    ContentBase base = new ContentBase(); 
    base.image = att.value; 
    returnList.Add(base); 
    } 
+1

'att.Value'는 문자열이 될 것이고'base.image'는 문자열 배열이 될 것이기 때문에'base.image = att.value;'는 잘못 될 것이라고 생각합니다. –

1

당신은 심지어 foreach를 사용하지 않고 Linq에 사용할 수 있습니다 이 같은 루프 :

List<ContentBase> returnList = htmlDocument.DocumentNode.SelectNodes("//img") 
    .Select(img => new ContentBase {images = new[] {img.Attributes["src"].Value}}).ToList();