c# - 从 SQL 表处理 mvc 中的多个字段
问题描述
在我拥有的 4 个表中,3 个包含相似的(client,policyno,policytype..)字段,但数据不同。然而,另一个表有非常不同的字段(客户、性别、电话、dob..)。我的视图中有一个搜索框,它使用电话号码来显示所有 3 个表中的相关记录。不幸的是,我无法处理第四个表的字段。不断返回错误
System.IndexOutOfRangeException: 'PolicyNo'
我的模型:
public class TableModels
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string Client { get; set; }
public string PolicyNo { get; set; }
public short? PolicyType { get; set; }
public string Telephone { get; set; }
//Health
public DateTime? DOB { get; set; }
public string Sex { get; set; }
public string DepMemberNumber { get; set; }
protected TableModels ReadValue(SqlDataReader reader)
{
TableModels obj = new TableModels();
if (reader["ID"] != DBNull.Value)
{
obj.ID = (int)reader["ID"];
}
if (reader["Client"] != DBNull.Value)
{
obj.Client = (string)reader["Client"];
}
if (reader["PolicyNo"] != DBNull.Value)
{
obj.PolicyNo = (string)reader["PolicyNo"];
}
if (reader["PolicyType"] != DBNull.Value)
{
obj.PolicyType = (short)reader["PolicyType"];
}
在受保护的 TableModel 类上返回错误。
任何意见,将不胜感激
看法:
@model IEnumerable
@foreach (var item in Model.OrderByDescending(m => m.Client))
{
<tr>
<td>
@Html.DisplayFor(modelitem => item.Client)
</td>
<td>
@Html.DisplayFor(modelitem => item.Telephone)
</td>
<td>
@Html.ActionLink("Details", "Details", new { id = item.Telephone })
</td>
</tr>
}
控制器:
public ActionResult Details(string id)
{
using (MainContext db = new MainContext())
{
if (id == null)
{
return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);
}
List<SingleView> x1 = db.SingleViews.Where(a => a.Telephone == id).ToList();
List<SingleViewM> x2 = db.SingleViewMs.Where(a => a.Telephone == id).ToList();
List<SingleViewWst> x3 = db.SingleViewWsts.Where(a => a.Telephone == id).ToList();
List<PensionsView> x4 = db.PensionsViews.Where(a => a.Telephone == id).ToList();
List<Health> x5 = db.Health.Where(a => a.Telephone == id).ToList();
SingleModel objview = new SingleModel();
objview.USSD = x1;
objview.Mombasa = x2;
解决方案
推荐阅读
- r - 如果存在,如何截断字符串的特定部分
- macos - 您如何描绘要导入 MacOS 笔记应用程序的文本?
- flutter - 更改材料按钮颜色 onPress 并返回默认 onLongPress
- google-apps-script - 在警报框的编辑触发器上
- r - 如何绘制具有重复 x 轴值的连续线
- python - 如何根据空白列删除 CSV 文件中的行
- javascript - JS中的数组仅运行最后一个方差值
- c# - 工作 1-2 小时后应用程序开始冻结且无响应
- python - 如何强制 python 在 macOS 上解锁文件
- flutter - 为 ListView.builder 中的每个孩子创建一个新的 BlocProvider 是不是一个坏主意?