我有RegisterModel
,我需要为SelectCountryID
和控制器传球被重视,但设置的默认值时,我非常喜欢这一点,我得到一个错误:“未设置为一个对象的实例对象引用”如何在模型中设置属性的默认值?
public class RegisterModel
{
public RegisterModel()
{
SelectCountryID=1;
}
public int SelectCountryID {get;set;}
}
控制器:
int test = Model.Register.SelectCountryID;
编辑:
IndexPageModel model = new IndexPageModel();
model.Register = new RegisterModel
{
int test = model.Register.SelectCountryId,
Country = new SelectList(manager.GetCountries(), "Id", "Name"),
WebTypeOfLicense = new SelectList(CommonDbService.GetAllLicense(), "Id", "Name"),
Region = new SelectList(manager.GetRegions(model.Register.SelectCountryId), "Id", "Name"),
};
您需要首先实例化对象... – Liam
[C#中的不理解对象实例化]的可能重复(http://stackoverflow.com/questions/888514/not-understandi ng-object-instantiation-in-c-sharp) – Liam