C# 使用属性时尝试从当前会话获取值
我是C#的新手,所以请对我宽容点 我有一个大问题,这几天来一直困扰着我 问题: 我们有一个web应用程序,使用MVC4,当打开一个文档时,模型中的所有值都是在backingstore会话中通过调用方法SaveValues()创建的 属于上述VisitViewModel类的每个类,其编码如下例所示。它们继承NotifyPropertyChangedBase(我不会在这里发布太多的类信息) 现在的问题是,在备份存储(会话)中创建的值如下所示(当我展开其中任何一个时,即ActivityVM,它包含我想要的键和值) 问题是我用来获取值和比较更改的数据的代码,无法找到值,因为它们存储为属性。。。。有人能提出解决这个问题的办法吗? 可能在保存值时,我可以循环遍历每个类,并将每个类中的所有值添加到备份存储中,从而停止将值另存为属性 从备份存储中获取值并执行比较的代码(查看数据是否已更改)C# 使用属性时尝试从当前会话获取值,c#,asp.net-mvc,inotifypropertychanged,C#,Asp.net Mvc,Inotifypropertychanged,我是C#的新手,所以请对我宽容点 我有一个大问题,这几天来一直困扰着我 问题: 我们有一个web应用程序,使用MVC4,当打开一个文档时,模型中的所有值都是在backingstore会话中通过调用方法SaveValues()创建的 属于上述VisitViewModel类的每个类,其编码如下例所示。它们继承NotifyPropertyChangedBase(我不会在这里发布太多的类信息) 现在的问题是,在备份存储(会话)中创建的值如下所示(当我展开其中任何一个时,即ActivityVM,它包含我想
public void OnPropertyChanged(字符串propName,对象propValue)
{
//如果有任何对象类型,请确保正确定义了Equals,以检查是否具有正确的唯一性。
if(HttpContext.Current.Session[“SbackingStore”]!=null)
{
如果(propValue==null)propValue=“”;
BackingStore=(Dictionary)HttpContext.Current.Session[“SbackingStore”];
if(BackingStore[propName].Equals(propValue))//此处有错误:给定的键在字典中不存在。
您有以下代码:
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”]会使用新backingstore获取ovveriden,这就是为什么您会得到“字典中缺少一个键”.我会认真重新考虑这里的基本方法…MVC是无状态的,这是逆流而上的。我也看到命名冲突的严重问题。你有什么建议吗?那将是重新设计你的应用程序…为什么你认为你需要保持所有的状态,如果有,为什么不使用MemCache?你的ORM是什么?嗨..我们从webservices获取数据,然后使用MVC显示它。我需要查看哪些数据发生了更改,这是因为我们在另一个数据库(在lotus notes中)中使用了审批流程这需要查看哪些字段和值发生了更改。一旦我有了这些数据,我就通过webservice调用将数据发送回。因此,我需要查看哪些数据发生了更改,目前我正在使用这种方法(这种方法有效,但不适用于我所解释的特定模型).我知道这不是最好的方法,但此时此刻我正在寻找一个快速解决方案。我已编辑了您的标题。请参见“”,其中的共识是“不,他们不应该”。不,抱歉,这不正确……每次打开文档时,它都会删除备份存储,然后创建一个新的备份存储……我正在寻找一种以不同的方式存储值的方法,而不是我上面提到的方式……值在那里,但我不知道如何获取每个属性中的值。。。
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; }
}
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); }
}
}
[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.
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;
}