我在我的CMS 3.0项目下面的代码对象引用不设置到对象的实例与目前
SurveyController.cs
private BuisnessSurveyEntities bsdb = new BuisnessSurveyEntities();
[HttpGet]
public ViewResult BizSurveyCDF()
{
var bquery = from b in bsdb.form_field
where b.ID != null // int
where b.DATID != null // int
where b.CAPTION != null // string
select new {b.ID, b.DATID, b.CAPTION};
ViewData["BZQUESTIONS"] = new SelectList(bquery,"ID","DATID","CAPTION");
return View();
}
form_field.cs模型
public virtual string ID {GET; SET;}
public virtual string DATID
{
get{return _dATID;}
set{_dATID = value;}
}
private string _dATID = "\'\'";
没有NULL字段
BizSurveyCDF.cshtml
@model IEnumberable<CMS.Models.form_field>
<table>
<tr>
<th>
DATID
</th>
<th>
CAPTION
</th>
</tr>
@foreach(var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.DATID)
</td>
<td>
@Html.DisplayFor(modelItem => item.CAPTION)
</td>
</tr>
}
</table>
现在我的问题是当我运行它时,出现以下错误:
对象引用未设置为对象的实例。
和出错行是 @foreach(以型号VAR项目)
我经历了整个表不见了,东西全部换成NULL值的和至今还在领受此错误。到目前为止,我读过的所有东西都说它有NULL值的问题,但正如我已经说过的,我已经摆脱了所有的空值。
任何帮助将非常感激。
感谢
难道'bsdb'是'null'? – Oded
可能是LINQ正在执行超出范围。所以我认为Oded很可能是对的。尝试对查询结果执行ToArray()或ToList(),然后将其交给视图? – Balthy
你有form_field.cs模型中的CAPTION吗? – Damith