[英]List Updating a value after Insert
i have a issue, I want to Update a value to element which is getting added in a list. 我有一个问题,我想将值更新为要添加到列表中的元素。
With generic example:- 与一般的例子:-
I have a Model Object:- 我有一个模型对象:
public class Model
{
public int ModelProperty1 { get; set; }
public int ModelProperty2 { get; set; }
public int ModelPropertyStatus { get; set; }
}
I have a DTO Object:- 我有一个DTO对象:
public class DTO
{
public int DTOProperty1 { get; set; }
public int DTOProperty2 { get; set; }
public int DTOPropertyStatus { get; set; }
}
Now, in my Controller i have a List which adds Model object:- 现在,在我的控制器中,我有一个添加模型对象的列表:
List<Model> _listOfModel = new List<Model>();
Secondly, i have created a mapping method which maps my Model & DTO 其次,我创建了一个映射模型和DTO的映射方法
private Model MapDTOToModel(DTO dto)
{
return new Model
{
ModelProperty1 = dto.DTOProperty1,
ModelProperty2 = dto.DTOProperty2
};
}
Coming to my Issue:- I want something like this to work:- //I want a piece of code that Updates my ModelPropertyStatus after it gets inserted to //List 即将出现我的问题:-我想要类似的东西工作:-//我想要一段代码,将其插入到// List之后更新我的ModelPropertyStatus
_listOfModel.Add(new Model() { ModelPropertyStatus = 1 });
//Here is the piece of code i want to convert:- //这是我要转换的代码:-
Model model = new Model();
model.ModelPropertyStatus = 1;
_listOfModel.Add(MapDTOToModel(model));
To be specific(Updated) I want something like this:- 具体来说(更新),我想要这样的东西:
_listOfModel.Add(MapDTOToModel() { ModelPropertyStatus = 1 });
Any Suggestions?? 有什么建议么??
You are looking for an ObservableCollection
: 您正在寻找一个
ObservableCollection
:
Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.
表示一个动态数据集合,当添加,删除或刷新整个列表时提供通知。
Here's an example: 这是一个例子:
public class Model
{
public int ModelProperty1 { get; set; }
public int ModelProperty2 { get; set; }
public int ModelPropertyStatus { get; set; }
}
void Main()
{
ObservableCollection<Model> _listOfModel = new ObservableCollection<Model>();
_listOfModel.CollectionChanged += (s, o) =>
{
foreach (var m in o.NewItems)
((Model)m).ModelPropertyStatus = 1;
};
var model = new Model();
Console.WriteLine("Before add: " + model.ModelPropertyStatus.ToString());
_listOfModel.Add(model);
Console.WriteLine("After add: " + model.ModelPropertyStatus.ToString());
}
output: 输出:
Before add: 0
添加前:0
After add: 1添加后:1
As you can see, using the CollectionChanged
event, the property gets updatet during the insert. 如您所见,使用
CollectionChanged
事件,该属性在插入过程中获取updatet。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.