首页 > 解决方案 > 从 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;

标签: c#sql-serverasp.net-mvc

解决方案


推荐阅读