0

我对 C# 很陌生,所以请放轻松。

我有一个重大问题困扰了我好几天。

问题:我们有一个 Web 应用程序并使用 MVC4,当打开一个文档时,模型中的所有值都通过调用方法 SaveValues() 在 session 的 backingstore 中创建

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.
4

1 回答 1

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;

    }

所有的类都用这个方法继承基类。因此,每当您在任何派生类上调用 SaveValues 方法时,HttpContext.Current.Session["SbackingStore"] 都会使用新的后备存储进行验证,这就是为什么您会得到“字典中缺少一个键”。

于 2013-07-03T14:27:18.293 回答