如何保存ViewModel/Model
传递给View
的信息?我需要将此信息保存到数据库中,但为了确保检索值,我设置了MessageBox
来显示是否正在检索该值。
这是我的ViewModel / Model的代码:
namespace OcelotPayroll
{
public class PayslipModel : EmployeeModel
{
private decimal statIncome;
private string totalDed;
private string _netpay;
private string _amount;
public string Amount
{
get
{
return _amount;
}
set
{
_amount = value;
OnPropertyChanged("Amount");
}
}
public decimal StautoryIncome
{
get
{
return statIncome;
}
set
{
statIncome = value;
}
}
public PayslipModel()
{
Date = DateTime.Today.Date;
SelectAll = new DelegateCommand(Select, () => CanSelect);
UnSelectAll = new DelegateCommand(UnSelect, () => CanUnSelect);
SaveToDatabase = new DelegateCommand(Save, () => CanSave);
scon = new MichaelAllenEntities();
}
public ICommand SaveToDatabase
{
get; set;
}
private bool CanSave
{
get { return Workspaces.Count > 0; }
}
private async void Save()
{
try
{
MessageBox.Show(StautoryIncome.ToString()); //returns 0
MessageBox.Show(Amount.ToString()); //NullPointerException
}
catch (DbEntityValidationException ex)
{
foreach (var en in ex.EntityValidationErrors)
{
var exceptionDialog = new MessageDialog
{
Message = { Text = string.Format("{0}, {1}", en.Entry.Entity.GetType().Name, en.Entry.State) }
};
await DialogHost.Show(exceptionDialog, "RootDialog");
foreach (var ve in en.ValidationErrors)
{
exceptionDialog = new MessageDialog
{
Message = { Text = string.Format("{0}, {1}", ve.PropertyName, ve.ErrorMessage) }
};
await DialogHost.Show(exceptionDialog, "RootDialog");
}
}
}
catch(Exception ex)
{
var exceptionDialog = new MessageDialog
{
Message = { Text = string.Format("{0}", ex) }
};
await DialogHost.Show(exceptionDialog, "RootDialog");
}
}
public async void CalcNis()
{
try
{
float nis = 0;
NisName = "N.I.S.";
SqlConnection con = new SqlConnection(scon.Database.Connection.ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select NIS from Deductions where TaxId='1'", con);
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
nis = float.Parse(sdr.GetValue(0).ToString());
}
con.Close();
if (Amount != string.Empty)
{
NisVal = (decimal.Parse(Amount.ToString()) * decimal.Parse(nis.ToString())).ToString("N2");
StautoryIncome = (decimal.Parse(Amount.ToString()) - decimal.Parse(NisVal.ToString())); //value gets assigned here
}
}
catch(Exception ex)
{
var exceptionDialog = new MessageDialog
{
Message = { Text = ex.ToString() }
};
await DialogHost.Show(exceptionDialog, "RootDialog");
}
}
}
}
Amount
的数据绑定:
<TextBlock Foreground="Black" Margin="0 0 0 0" VerticalAlignment="Top" Grid.Column="3" Text="{Binding Path=Amount, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
您看到的所有计算都正常工作,所有值都完美地传递给View
。但是,当我尝试保存时,我会收到Object Reference not set to instance of an object
Exception
。
如何从视图中检索值以保存到数据库?
修改:好的,我已经更新了代码,使其更容易理解。我在此行MessageBox.Show(StautoryIncome.ToString());
收到错误。它保存在CalcNis()
方法中计算后的值,并使用相同的值执行其他计算。但是,一旦我尝试保存,它就像StautoryIncome
返回0并且任何数据绑定都返回NullPointerException
。 Amount
的绑定工作完美,只要我开始在Textbox
中输入文字,TextBlock
更新的值就相同。
答案 0 :(得分:0)
这个问题原来是UserControls没有使用ViewModel的同一个实例。讨论转移到:Unable to retrieve binding values MVVM,它已经解决了:)。希望有人觉得它很有用。