我是C#的新手,所以请放轻松我。
我有一个主要问题,一直困扰着我好几天。
问题: 我们有一个Web应用程序并使用MVC4,当打开文档时,通过调用方法SaveValues()
,在会话中的后台存储中创建模型中的所有值。public class NotifyPropertyChangedBase : INotifyPropertyChanged
{
public Dictionary<string, object> BackingStore = new Dictionary<string, object>();
public Dictionary<string, object> Changes = new Dictionary<string, object>();
public bool HasChanges { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public void SaveValues()
{
// Expensive, to use reflection, especially if LOTS of objects are going to be used.
// You can use straight properties here if you want, this is just the lazy mans way.
this.GetType().GetProperties().ToList().ForEach(tProp => { BackingStore[tProp.Name] = tProp.GetValue(this, null); Changes[tProp.Name] = ""; });
HttpContext.Current.Session["SbackingStore"] = BackingStore;
HasChanges = false;
}
public void RevertValues()
{
// Again, you can use straight properties here if you want. Since this is using Property setters, will take care of Changes dictionary.
this.GetType().GetProperties().ToList().ForEach(tProp => tProp.SetValue(this, BackingStore[tProp.Name], null));
HasChanges = false;
}
public void OnPropertyChanged(string propName, object propValue)
{
// If you have any object types, make sure Equals is properly defined to check for correct uniqueness.
if (HttpContext.Current.Session["SbackingStore"] != null)
{
if (propValue == null) propValue = "";
BackingStore = (Dictionary<string, object>)HttpContext.Current.Session["SbackingStore"];
if (BackingStore[propName].Equals(propValue))
{ }
else
{
Changes[propName] = propValue;
HasChanges = true;
}
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
}
我有类似的设置,其中包含;
public class VisitViewModel : NotifyPropertyChangedBase
{
public Activity ActivityVM { get; set; }
public VBSSteps VBSStepsVM { get; set; }
public ProductTime ProductTimeVM { get; set; }
public OtherPST OtherPSTVM { get; set; }
public TimeRange TimeRangeVM { get; set; }
}
属于上述VisitViewModel类的每个类都编码如下例所示。他们继承了NotifyPropertyChangedBase(我不会在这里发布所有类的信息太多);
public class Activity : NotifyPropertyChangedBase
{
public int Id { get; set; }
public string NotesID { get; set; }
public string SubID { get; set; }
public string Form { get; set; } // Form Name
private string _custNumber;
[MobileCRM.Resources.LocalizedString.LocalizedDisplayName("CustomerNumber")]
[DataType(DataType.Text)]
[Required]
public string CustNumber
{
get { return _custNumber; }
set { _custNumber = value; OnPropertyChanged("CustNumber", value); }
}
private string _companyName;
[MobileCRM.Resources.LocalizedString.LocalizedDisplayName("CustomerName")]
[DataType(DataType.Text)]
[Required]
public string CompanyName
{
get { return _companyName; }
set { _companyName = value; OnPropertyChanged("CompanyName", value); }
}
}
现在的问题是,在后备存储(会话)中创建的值如下所示(当我展开其中任何一个,即ActivityVM时,包含我想要的键和值。);
[0] {[ActivityVM, MobileCRM.Models.Activity]}
[1] {[VBSStepsVM, MobileCRM.Models.VBSSteps]}
[2] {[ProductTimeVM, MobileCRM.Models.ProdcutTime]}
[3] {[OtherPSTVM, MobileCRM.Models.OtherPST]}
[4] {[TimeRangeVM, MobileCRM.Models.TimeRange]}
[5] {[HasChanges, False]}
这个问题是我用来获取值并比较更改后的数据的代码,无法找到值,因为它们存储为属性....有人可以建议解决这个问题吗? 也许当保存值时,我可以循环遍历每个类并将每个类中的所有值添加到后备存储,因此停止将值保存为属性。
从后备存储获取值并执行比较的代码(以查看数据是否已更改)
public void OnPropertyChanged(string propName, object propValue)
{
// If you have any object types, make sure Equals is properly defined to check for correct uniqueness.
if (HttpContext.Current.Session["SbackingStore"] != null)
{
if (propValue == null) propValue = "";
BackingStore = (Dictionary<string, object>)HttpContext.Current.Session["SbackingStore"];
if (BackingStore[propName].Equals(propValue)) // Errors here : gives The given key was not present in the dictionary.
答案 0 :(得分:0)
您有以下代码:
public void SaveValues()
{
// Expensive, to use reflection, especially if LOTS of objects are going to be used.
// You can use straight properties here if you want, this is just the lazy mans way.
this.GetType().GetProperties().ToList().ForEach(tProp => { BackingStore[tProp.Name] = tProp.GetValue(this, null); Changes[tProp.Name] = ""; });
HttpContext.Current.Session["SbackingStore"] = BackingStore;
HasChanges = false;
}
所有clases都使用此方法继承基类。因此,无论何时在任何派生类上调用SaveValues方法,HttpContext.Current.Session [“SbackingStore”]都会使用新的后备存储获取ovveriden,这就是为什么你会得到“字典中缺少一个键”。