我有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