背景
我目前正在尝试向模型列表中添加一个值,但这似乎与我在 JavaScript 中的做法不同。
型号
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
}
问题
我正在尝试将 HtmlAttribute att = img.Attributes["src"];
的结果添加到我列表的 string[] images
中。这样,一旦我完成此迭代,我将能够在我的列表的 string[] images
部分中获得我从 for each 循环中获得的所有值
更新
这是填充列表中其他项目的前面的函数
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,
});
最佳答案
这里的returnList
是ContentBase
的List,所以它的项应该是相应类的对象。因此向其中添加项目的代码如下所示:
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}}
}
关于c# - 向基于模型的列表添加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38378996/