我试图通过添加以下行添加到现有MVC站点上的表的另一列:
Model代码片段:
public partial class viewUserRecord
{
[Display(Name = "Agent ID")]
public Nullable<int> agentId { get; set; }
...
....
// New code:
[Display(Name = "Mailing Zip")]
public string mailingZip { get; set; }
}
视图:
<tr role="row">
<th class="sorting" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" aria-label="Rendering engine: activate to sort column ascending">
@Html.DisplayNameFor(model => model.agentId)
</th>
...
....
<th class="sorting" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" aria-label="Rendering engine: activate to sort column ascending">
@Html.DisplayNameFor(model => model.mailingZip)
</th>
</tr>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.agentId) // debugger shows correct value here
</td>
...
....
<td>
@Html.DisplayFor(modelItem => item.mailingZip) // debugger shows NULL here
</td>
</tr>
}
</tbody>
将新列添加到表中,但不检索相关数据。
模型引用了数据库视图viewUserDetails
,其中包含了新字段mailingZip
的正确数据,但通过该视图进行调试时显示的内容为NULL
。
Agent ID
字段显示fine。
我意识到我错过了一些东西,但却感到困惑。
你试过了吗
[Display(Name = "Agent ID")]
public int id { get; set; }
不是public int id { get; set; }
[Display(Name = "Agent ID")]
?
直接替换
@Html.DisplayFor(modelItem => item.agentId)
@item.agentId
感谢大家的建议,但这毕竟是一个疏忽。
原来我忽略的步骤是需要更新.edmx
文件以反映新列。
这反过来又被证明远非直截了当,但如果我不能让它工作,我会创建一个新的问题。