如何显示相关数据?

时间:2012-12-11 12:13:03

标签: asp.net-mvc-4

我的表格包含以下列:CustomerInfo(customer_Id, Name, address) 另一个包含以下列的表:Custoffice(EmployeeId,Customer_Id,designation) 另一个包含以下列的表:CustBankinfo(Customer_Id,depositdate,retrievedate)

使用这个我创建了一个表单,当我输入Customer_Id并单击提交按钮时,我应该在asp.net中使用mvc4获取所有3个表。 我尝试过在Contosouniversity示例中使用流畅的api,但是我收到了错误。

我的代码如下..

模型部分:

 CustomerInformation.cs


 namespace Mvcnew.Models
 {
  public class CustomerInfo
  {
   [DatabaseGenerated(DatabaseGeneratedOption.None)]
   public int customer_Id{get;set;}
   public string  Name {get;set;}
   public string  address{get;set;}
  }
  public class Custoffice
  {
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int EmployeeId {get;set;}
    public int Customer_Id {get;set;}
    public string designation{get;set;}
  }
  public class  CustBankinfo
  {
   [DatabaseGenerated(DatabaseGeneratedOption.None)]
   public int Customer_Id {get;set;}
   public int depositdate {get;set;}
   public int retrievedate {get;set;}
  }
}

    CustomerContext.cs

        namespace Mvcnew.Models
        {
          public class CustomerContext : DbContext
          {
            public DbSet<CustomerInfo> customerinfo{ get; set; }
            public DbSet<CustOffice> customeroffice{ get; set; }
            public DbSet<CustBankinfo> custbankinfo{ get; set; }

            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
              modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
              modelBuilder.Entity<CustomerInfo>().HasOptional(p => p.customeroffice).WithRequired(p=>p.customerinfo);
              modelBuilder.Entity<CustomerInfo>().HasOptional(p => p.custbankinfo).WithRequired(p=>p.customerinfo);
            } 

          }
       }



     Mappingdata.cs
       namespace Mvcnew.ViewModels
       {              
        public class MppingData
        {
         public IEnumerable<CustomerInfo> customerinfo{ get; set; }
         public IEnumerable<CustOffice> customeroffice{ get; set; }
        public IEnumerable<CustBankinfo> custbankinfo{ get; set; } 
        }
       }

我的控制器部分如下           控制器部分:

   Controller action as
   using Mvcnew.Models;
   using Mvcnew.ViewModels;
    namespace Mvcnew.Controllers
    {
     public class MappingController : Controller
     {
       CustomerEntities db=new CustomerEntities();            
        public ActionResult SearchIndex(int? id)
        {
        var viewmodel = new MppingData();
        viewmodel.CustomerInfo=db.customerInfo.Include(i=>i.CustOffice);
        if(id!=null)
        {
         ViewBag.Customer_Id  = id.Value; 
         viewModel.CustOffice = viewModel.CustomerInfo.Where(i => i.Customer_Id== id.Value).Single().CustOffice;
         viewModel.CustBankinfo= viewModel.CustomerInfo.Where(i => i.Customer_Id== id.Value).Single().CustBankinfo;

        }
        return view(viewmodel);
       }
    }

这是我的整体代码。我卡在控制器附近,因为它显示错误无法转换。

0 个答案:

没有答案