在同一视图中调用多个SQL表

时间:2014-10-01 19:38:33

标签: c# sql asp.net-mvc

我需要在一个视图中显示多个表。这似乎是一个问题,因为视图只允许我可以告诉的一个模型定义。我尝试过实施解决方案,但都没有成功。 具体来说,我收到错误消息:"传递到字典中的模型项的类型是' 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]' "

视图

@model IEnumerable<BillingApp.Models.tbl1join>

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

~excluding tables because there are too many fields~


</body></html>   
  }

加入两个表(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; }
}

}

模型定义:

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

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

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; }
}
}

Controller(BillingController.cs)

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


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

更新

在控制器中添加了tbl1join作为返回类型,但是给出了一个错误,说它是一个用作变量的类型。

  public ActionResult HeaderRecordTable1()
    {
        {
            return View(IEnumerable<tbl1join>);
        }
    }

2 个答案:

答案 0 :(得分:1)

将其包装到ViewModel

public class RecordVM
{
    public HEADER_RECORD header { get; set; }
    public HEADER_EXTENSION_RECORD ext { get; set; }
}

return View(new RecordVM { header = db.HEADER_RECORD, ext = db.HEADER_EXTENSION_RECORD });

答案 1 :(得分:0)

错误消息表明您传入的类型错误,您需要查询数据库并创建您的视图所期望的合适模型。看起来您正在尝试使用视图模型tbl1join将两个模型传递给视图。

您可以按如下方式为指定的id值填充视图模型:

public ActionResult ViewModelExtension(int id)
{
    var viewModel = new tbl1join();

    viewModel.HEADER_RECORD = db.HEADER_RECORD.Where(h => h.id == id).SingleOrDefault;
    viewModel.HEADER_EXTENSION_RECORD = db.HEADER_EXTENSION_RECORD.Where(h => h.id == id).SingleOrDefault;

    return View(viewModel);
}

如果两个模型之间没有某种关系,我会感到惊讶。如果在你的2个模型中指定了这个,那么上面的内容可能会有所不同。