一个视图中的多个表

时间:2014-09-23 17:43:57

标签: c# sql razor

我有一个模型(Model2),它包含我所有SQL表的定义。我需要创建一个视图,在其中我显示来自多个不同表的数据。这些是我想在一个视图中加入的模型

HEADER_RECORD.cs

namespace BillingApp.Models
{
using System;
using System.Collections.Generic;

public partial class HEADER_RECORD
{
    public int HRID { get; set; }
    public string TABLE_NUMBER { get; set; }
    public string COMPANY { get; set; }
    public string STATE_CODE { get; set; }
    public string BILL_CODE { get; set; }
    public string RECORD_TYPE { get; set; }
    public string MASK_EXTENSION_ID { get; set; }
    public string OVERPAYMENT_LIMIT { get; set; }
    public string UNDERPAYMENT_LIMIT { get; set; }
    public string REFUND_ACTION_OVR { get; set; }
    public string REFUND_ACTION_PAR { get; set; }
    public string REFUND_ACTION_RTN_PRM { get; set; }
    public string REFUND_ACTION_CNC { get; set; }
    public string EFT_PAC_OPTION { get; set; }
    public string EFT_PAC_NOTICE { get; set; }
    public string EFT_PAC_NSF_LIMIT { get; set; }
    public string PREMIUM_ROUNDING { get; set; }
    public string DB_CC_OPTION { get; set; }
    public string NSF_CHECK_LIMIT { get; set; }
    public string NSF_CHECK_OPTION { get; set; }
    public string FIRST_TERM_BILLING { get; set; }
    public string CARRY_DATE_OPTION { get; set; }
    public string ENDORSEMENT_DAYS { get; set; }
    public string DATE_METHOD { get; set; }
    public string RENEWAL_OPTION { get; set; }
    public string DROP_DAYS { get; set; }
    public string MULTI_PAY_IND { get; set; }
    public string MINIMUM_INSTALLMENT { get; set; }
    public string ENDORSEMENT_ACTION { get; set; }
    public string I_OR_S_OPTION_DAYS { get; set; }
    public string S_OPTION_PERCENT { get; set; }
    public string SERVICE_CHARGE_PREPAID { get; set; }
    public string REINSTATE_OPTION { get; set; }
    public string CASH_WITH_APPLICATION { get; set; }
    public string DB_CC_NOTICE { get; set; }
    public string DOWN_PAY_DAYS { get; set; }
    public string MONTH_BY_TERM { get; set; }
    public string LEAD_MONTHS { get; set; }
    public string INITIAL_MONTHS { get; set; }
    public string DB_CC_REJECTS { get; set; }
    public string RETURN_ENDORSEMENT_OPTION { get; set; }
    public string RETURN_SPLIT_OPTION_PERCENT { get; set; }
    public string AUTOMATED_REFUND_DAYS { get; set; }
    public string RENEWAL_OPTION_BILL_PLAN { get; set; }
    public string EFFECTIVE_DATE { get; set; }
    public string MISC_DATA { get; set; }
    public string MISC_DATA2 { get; set; }
}
}

HEADER_EXTENSION_RECORD.cs

public partial class HEADER_EXTENSION_RECORD
{
    public int ERID { get; set; }
    public string ETABLE_NUMBER { get; set; }
    public string ECOMPANY { get; set; }
    public string ESTATE_CODE { get; set; }
    public string EBILL_CODE { get; set; }
    public string ERECORD_TYPE { get; set; }
    public string EMASK_EXTENSION_ID { get; set; }
    public string OVERPAYMENT_TOLERANCE_PERCENT { get; set; }
    public string UNDERPAYMENT_TOLERANCE_PERCENT { get; set; }
}
}

我的视图顶部(HeaderRecordTable1)

@model IEnumerable<BillingApp.Models.HEADER_RECORD>

@{
ViewBag.Title = "TABLE 01 DISPLAY";
Layout = "../Shared/Layout2.cshtml";    
}
@section featured2 {

更新:我创建了一个名为tbl1join的类,它有定义我要加入的两个表的子类。我在视图中引用了这个类(tbljoin.cs),但它不再识别我正在调用的任何字段名。 “CS1061:'BillingApp.Models.tbl1join'不包含'COMPANY'的定义,并且没有扩展方法'COMPANY'接受类型为'BillingApp.Models.tbl1join'的第一个参数'(您是否缺少using指令或汇编参考?)“

tbl1join.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BillingApp.Models
{
public class tbl1join
{
    public partial class HEADER_RECORD
    {
        public int HRID { get; set; }
        public string TABLE_NUMBER { get; set; }
        public string COMPANY { get; set; }
        public string STATE_CODE { get; set; }
        public string BILL_CODE { get; set; }
        public string RECORD_TYPE { get; set; }
        public string MASK_EXTENSION_ID { get; set; }
        public string OVERPAYMENT_LIMIT { get; set; }
        public string UNDERPAYMENT_LIMIT { get; set; }
        public string REFUND_ACTION_OVR { get; set; }
        public string REFUND_ACTION_PAR { get; set; }
        public string REFUND_ACTION_RTN_PRM { get; set; }
        public string REFUND_ACTION_CNC { get; set; }
        public string EFT_PAC_OPTION { get; set; }
        public string EFT_PAC_NOTICE { get; set; }
        public string EFT_PAC_NSF_LIMIT { get; set; }
        public string PREMIUM_ROUNDING { get; set; }
        public string DB_CC_OPTION { get; set; }
        public string NSF_CHECK_LIMIT { get; set; }
        public string NSF_CHECK_OPTION { get; set; }
        public string FIRST_TERM_BILLING { get; set; }
        public string CARRY_DATE_OPTION { get; set; }
        public string ENDORSEMENT_DAYS { get; set; }
        public string DATE_METHOD { get; set; }
        public string RENEWAL_OPTION { get; set; }
        public string DROP_DAYS { get; set; }
        public string MULTI_PAY_IND { get; set; }
        public string MINIMUM_INSTALLMENT { get; set; }
        public string ENDORSEMENT_ACTION { get; set; }
        public string I_OR_S_OPTION_DAYS { get; set; }
        public string S_OPTION_PERCENT { get; set; }
        public string SERVICE_CHARGE_PREPAID { get; set; }
        public string REINSTATE_OPTION { get; set; }
        public string CASH_WITH_APPLICATION { get; set; }
        public string DB_CC_NOTICE { get; set; }
        public string DOWN_PAY_DAYS { get; set; }
        public string MONTH_BY_TERM { get; set; }
        public string LEAD_MONTHS { get; set; }
        public string INITIAL_MONTHS { get; set; }
        public string DB_CC_REJECTS { get; set; }
        public string RETURN_ENDORSEMENT_OPTION { get; set; }
        public string RETURN_SPLIT_OPTION_PERCENT { get; set; }
        public string AUTOMATED_REFUND_DAYS { get; set; }
        public string RENEWAL_OPTION_BILL_PLAN { get; set; }
        public string EFFECTIVE_DATE { get; set; }
        public string MISC_DATA { get; set; }
        public string MISC_DATA2 { get; set; }
    }
     public partial class HEADER_EXTENSION_RECORD
{
    public int ERID { get; set; }
    public string ETABLE_NUMBER { get; set; }
    public string ECOMPANY { get; set; }
    public string ESTATE_CODE { get; set; }
    public string EBILL_CODE { get; set; }
    public string ERECORD_TYPE { get; set; }
    public string EMASK_EXTENSION_ID { get; set; }
    public string OVERPAYMENT_TOLERANCE_PERCENT { get; set; }
    public string UNDERPAYMENT_TOLERANCE_PERCENT { get; set; }
}
}

}

HeaderRecordTable1.cshtml

model IEnumerable<BillingApp.Models.tbl1join>

@{
ViewBag.Title = "TABLE 01 DISPLAY";
Layout = "../Shared/Layout2.cshtml";    
}
@section featured2 {

更新:我现在有tbl1join.cs,如下所示

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BillingApp.Models
{
public class tbl1join
{
    public HEADER_RECORD HeaderRecord { get; set; }
    public HEADER_EXTENSION_RECORD ExtensionRecord { get; set; }
}

}

在视图中我有

@model IEnumerable<BillingApp.Models.tbl1join>

@{
ViewBag.Title = "TABLE 01 DISPLAY";
Layout = "../Shared/Layout2.cshtml";    
}
@section featured2 {

调用foreach中的字段

@foreach (var item in Model)
    {         
        <tr>
            <td>
            <p class="one">@Html.DisplayFor(modelItem => item.HeaderRecord.COMPANY )</p>
        </td>

但是我收到以下错误:“传入字典的模型项的类型为'System.Data.Entity.DbSet 1[BillingApp.Models.HEADER_RECORD]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1 [BillingApp.Models.tbl1join]'。”

更新:仍然遇到此问题 - 我的控制器代码如下 控制器代码:

 public ActionResult HeaderRecordTable1()
    {
        {
            return View(db.HEADER_RECORD);
        }
    }

    public ActionResult HeaderExtensionRecord()
    {
        {
            return View(db.HEADER_EXTENSION_RECORD);
        }
    }

1 个答案:

答案 0 :(得分:0)

从你的tbl1join类中取出你的HEADER_RECORD和HEADER_EXTENSION_RECORD类。如果tbl1join是您的viewmodel,请为其提供所需的属性,并从HEADER_RECORD和HEADER_EXTENSION_RECORD类的实例中填充它们。否则,你的tbl1join类也可能如下所示:

public class tbl1join
 {
   public HEADER_RECORD HeaderRecord {get;set;}
   public HEADER_EXTENSION_RECORD ExtensionRecord {get;set;}
 }

HEADER_RECORD和HEADER_EXTENSION_RECORD成为viewmodel的属性,而不是嵌套类。

有意义吗?