我的知識是.Net缺乏巨大的。在之後的任何援助,將不勝感激:使用實體框架和MVC3搜索記錄
我有一個客戶型號:
public class CustomerModel
{
private DBEntities db = new DBEntities();
public List<CustomerModel> CustomerResultModel { get; set; }
[Required]
[DisplayName("Customer Number")]
public long ID { get; set; }
[StringLength(50)]
public string Firstname { get; set; }
[StringLength(50)]
public string Organisation { get; set; }
[StringLength(500)]
[DisplayName("Address Line 1")]
public string AddressLine1 { get; set; }
[StringLength(50)]
[Required(ErrorMessage = "A Postcode is required")]
public string Postcode { get; set; }
public CustomerModel GetCustomerResults(string q)
{
CustomerModel model = new CustomerModel();
var res = from s in db.CMUCustomers select s;
foreach (var result in res)
{
CustomerModel modelres = new CustomerModel();
modelres.ID = result.ID;
modelres.CustomerName = result.Firstname;
modelres.AddressLine1 = result.AddressLine1;
modelres.Postcode = result.Postcode;
modelres.Organisation = result.Organisation;
model.CustomerResultModel.Add(modelres);
}
return model;
}
}
在我的控制器,我有:
private CustomerModel customerResults = new CustomerModel();
public ViewResult Search(string q)
{
CustomerModel model = customerResults.GetCustomerResults(q);
return View(model);
}
然而,我得到「model.CustomerResultModel錯誤。加入(modelres);」說明'對象引用未設置爲對象的實例'。任何建議,我可能做錯了什麼?
感謝
你在哪裏添加這條語句? 'private CustomerModel customerResults = new CustomerModel(); ' – TRR