使用MVC4中的单一视图将数据插入多个表? [英] insert data into multiple tables using Single View in MVC4?

查看:97
本文介绍了使用MVC4中的单一视图将数据插入多个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海友。我是MVC4的新手。目前我正在一个项目中工作。我需要创建Customer Master。我设计了Sql表。我的Sql表是完全标准化的,我使用了一些主键参考。我将用例子解释我的问题。

我的表格包含

1)客户名称,2)Noof合作伙伴,3)CustomerTypeID,4)SalutationID,5)Street ,6)位置,7)地点,8)AreaID,9)CityID,10)StateId,11)countryID,12)PinCode。

我想把所有的字段都放在单一的视图。但如果我点击保存按钮,它会将数据保存到多个表格。



我的数据库是

< pre> 客户 
CustomerID 主要 key Uniqueidentifier null
显示名称 Varchar 100 null
打印名称 Varchar 100 null
客户类型 ID Uniqueidentifier null

CustomerType >
客户类型ID 主要 密钥 Uniqueidentifier null
显示名称 Varchar 100 null ,,
PrintName Varchar 100 null
UnderCustomerTypeID Uniqueidentifier null

客户地址
CustomerAddressID 主要 密钥 Uniqueidentifier null
CustomerID Uniqueidentifier null
添加ressID Uniqueidentifier null

地址
AddressID 主要 密钥 Uniqueidentifier not null
AddressTypeID Uniqueidentifier < span class =code-keyword> null ,
DisplayName Varchar 100 null
SalutationID Uniqueidentifier null
PrintName Varchar 100 null
SimpleMode 位<​​/ span> null
DetailMode bit null
Street Varchar 100 null
位置 Varchar 100 null
放置 Varchar 100 null
AreadID Uniqueidentifier null
Pincode Varchar 100 null

区域
AreaID 主要 key Uniqueidentifier null
DisplayName Varchar 100 null

PrintName Varchar 100 null
CityID Uniqueidentifier null < /跨度>。

称呼
SalutationID 主要 Uniqueidentifier null
DisplayName Varchar 100 null
PrintName Varchar 100 null

城市
CityID 主要 key Uniqueidentifier null
DisplayName Varchar 100 null
PrintName Varchar 100 )< span class =code-keyword> null

StateID Uniqueidentifier null

状态
StateId 主要 Uniqueidentifier null
DisplayName Varchar 100 null
PrintName Varchar 100 null
CountryID Uniqueidentifier null

国家/地区
CountryID 主要 密钥 Uniqueidentifier null
DisplayName Varchar 100 null
PrintName Varchar 100 )< span class =code-keyword> null ,





我完全设计数据库设计。接下来,我开始在Visiual Studio 2012 Express For Web中设计表单。

1)我将数据库与Visiual Studio 2012 Express For web连接。

2)我创建了EDMX文件

3)我创建了模型。



我的模型是

Quote:

引用:



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

名称空间Sample_Customer.Models

{

公共类CustomerModel

{

公共类客户

{

public System.Guid CustomerID {get;组; }

public Nullable< system.guid> AccountsLedgerID {get;组; } $ / $
公共字符串DisplayName {get;组; }

公共字符串PrintName {get;组; }

public Nullable< system.guid> CustomerTypeID {get;组; }

}

公共部分类CustomerType

{

public System.Guid CustomerTypeID {get;组; } $ / $
公共字符串DisplayName {get;组; }

公共字符串PrintName {get;组; }

public Nullable< system.guid> UnderCustomerTypeID {get;组; }

}

公共部分类CustomerAddress

{

public System.Guid CustomerAddressID {get;组; }

public Nullable< system.guid> CustomerID {get;组; }

public Nullable< system.guid> AddressID {get;组; }

}

公共部分类AddressType

{

public System.Guid AddressTypeID {get;组; } $ / $
公共字符串DisplayName {get;组; }

公共字符串PrintName {get;组; }

}



公共部分班级地址

{

公共系统。 Guid AddressID {get;组; }

public Nullable< system.guid> AddressTypeID {get;组; } $ / $
公共字符串DisplayName {get;组; }

public Nullable< system.guid> SalutationID {get;组; }

公共字符串PrintName {get;组; }

public string Street {get;组; }

public string Location {get;组; }

public string Place {get;组; }

public Nullable< system.guid> AreaID {get;组; } $ / $
公共字符串PinCode {get;组; }

}公共部分课程称呼

{

public System.Guid SalutationID {get;组; } $ / $
公共字符串DisplayName {get;组; }

公共字符串PrintName {get;组; }

}公共部分类区域

{

public System.Guid AreaID {get;组; } $ / $
公共字符串DisplayName {get;组; }

公共字符串PrintName {get;组; }

public Nullable< system.guid> CityID {get;组; }

} public partial class City

{

public System.Guid CityID {get;组; } $ / $
公共字符串DisplayName {get;组; }

公共字符串PrintName {get;组; }

public Nullable< system.guid> StateID {get;组; }

}

公共部分类状态

{

public System.Guid StateID {get;组; } $ / $
公共字符串DisplayName {get;组; }

公共字符串PrintName {get;组; } $ / $
public Nullable< system.guid> CountryID {get;组; }

} public partial class Country

{

public System.Guid CountryID {get;组; } $ / $
公共字符串DisplayName {get;组; }

公共字符串PrintName {get;组; }

}





然后我创建了ViewModel将单个表格中的所有字段带来



引用:

使用System;

使用System.Collections.Generic;

使用System.Data.Entity;

使用System.Linq;

使用System.Web;

名称空间Sample_Customer 。模型

{

公共类CustomerViewModel

{

public System.Guid CustomerID {get;组; } $ / $
公共字符串CustomerName {get;组; } $ / $
public System.Guid CustomerTypeID {get;组; } $ / $
公共字符串CustomerType {get;组; } $ / $
公共字符串AddressTypeID {get; set;

public string SalutationID {get;组; }

public string Street {get;组; }

public string Location {get;组; }

public string Place {get;组; }

公共字符串AreaID {get;组; }

公共字符串CityID {get;组; }

公共字符串StateID {get;组; }

公共字符串CountryID {get;组; } $ / $
公共字符串PinCode {get;组; }



然后我创建了DBContect

公共类VisitorsEntities:DbContext

{

public DbSet< customer>客户1 {得到;组; }

public DbSet< customertype> CustomerType {get;组; }

public DbSet< customeraddress> CustomerAddress {get;组; }

public DbSet

Address {get;组; }

public DbSet< addresstype> AddressType {get;组; }

public DbSet< salutation>称呼{得到;组; }

public DbSet< Area>区域{get;组; }

public DbSet< city>城市{get;组; }

public DbSet< state>国家{get;组; }

public DbSet< country>国家{get;组; }



}

}

}





然后我根据ViewModel创建了控制器。

我的控制器是



引用:

使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Web.Mvc;

使用Sample_Customer.Models;



名称空间Sample_Customer.Controllers

{

公共类CustomerController:Controller

{

VisitorsEntities db = new VisitorsEntities();



// GET:/ Customer /



public ActionResult Index ()

{

返回查看();

}

公共ActionResult客户()

{

返回查看();

}

[HttpPost]

public ActionResult Customer(CustomerViewModel viewmodel)

{

var Customer =新客户()

{

CustomerID = Guid .NewGuid(),

DisplayName = viewmodel.CustomerName,

PrintName = viewmodel .CustomerName

};



var CustomerType = new CustomerType()

{

DisplayName = viewmodel.CustomerType,

PrintName = viewmodel.CustomerType

};

var Salutation = new Salutation( )

{

DisplayName = viewmodel .Salutation,

PrintName = viewmodel.Salutation。

}

var区域=新区域()

{

DisplayName = viewmodel.Area,

PrintName = viewmodel.Area
};



var City = new City()

{

DisplayName = viewmodel.City,

PrintName = viewmodel.City,

};

var State = new State()

{

DisplayName = viewmodel.State,

PrintName = viewmodel.State

};

var Country =新国家()

{

DisplayName = viewmodel.Country,

PrintName = viewmodel.Country

} ;

var地址=新地址()

{

// AddressTypeID = viewmodel .AddressType.ToString,

// SalutationID = viewmodel .Alias.ToString



DisplayName = viewmodel.CustomerName,

PrintName = viewmodel.CustomerName,

Street = viewmodel .Stree t,

位置= viewmodel.Location,

Place = viewmodel.Place,

};

var AddressType = new AddressType()

{

DisplayName = viewmodel .AddressType,

PrintName = viewmodel .AddressType

} ;

db.Customer.Add(Customer);

db.CustomerType.Add(CustomerType);

db.Address.Add(Address );

db.AddressType.Add(AddressType);

db.Salutation.Add(Salutation);

db.Area.Add(区域);

db.City.Add(City);

db.State.Add(State);

db.Country.Add (国家);



返回查看();

}

}

}







这里只有我的问题开始了。 Upto Model I创建了正确。问题是ViewModel和Controller。

我的要求是我想把所有这些字段都以单一形式出现。那是

MainForm



客户ID,客户名称,NoofPartners,称呼,街道,地点,位置,区域,城市,州,国家,Pincode。



子表格

区域表格

区域

打印名称

城市(下拉菜单)



CityForm

CityName

州(DropDown)



州表格

StateName

国家(DropDown)



国家表格

CountryName



就像我想要的那样。客户类型,区域,城市,州,国家都是单独的形式。而且地址类型是3个单选按钮,必须保留在主窗体中。我只想将每个表单ID引用到主窗体。为此我如何创建视图模型和控制器。当我点击保存按钮时它必须将数据保存在多个表中。我试着用我的级别来解释我的问题。



谢谢

解决方案

看看这个用于创建局部视图



ASP.NET MVC 4中的部分视图 [ ^ ]


这些CRUD链接运营







使用实体框架5在MVC 4中完成CRUD操作,无需编写单行代码 [ ^ ]



使用VS2013 MVC 5的新脚手架功能轻松实现CRUD操作 [ ^ ]


Hai Friends. I am very new to MVC4 . Currently i am working in one project. I need to create Customer Master. I designed the Sql Table. My Sql Table is Fully Normalized and I used some Primary Key References. I will explain my problem with Example.
My Form Contain
1)CustomerName ,2)Noof Partners,3)CustomerTypeID, 4)SalutationID, 5)Street, 6)Location, 7)Place, 8)AreaID, 9)CityID,10)StateId,11)countryID,12)PinCode.
I want to make this all Fields in Single View. But If I click the Save Button it will save the data into multiple Form.

My DB are

<pre>Customer 
CustomerID Primary key Uniqueidentifier not null,
Display Name Varchar(100) null,
Print Name Varchar(100) null,
Customer Type ID Uniqueidentifier null.

CustomerType>
Customer TypeID Primary key Uniqueidentifier not null,
Display Name Varchar(100) null,,
PrintName Varchar(100) null,
UnderCustomerTypeID Uniqueidentifier null

Customer Address
CustomerAddressID Primary key Uniqueidentifier not null,
CustomerID  Uniqueidentifier null,
AddressID Uniqueidentifier null,

Address
AddressID Primary key Uniqueidentifier not null,
AddressTypeID Uniqueidentifier null,
DisplayName Varchar(100) null,
SalutationID Uniqueidentifier null,
PrintName Varchar(100) null,
SimpleMode bit null,
DetailMode bit null,
Street Varchar(100) null,
Location Varchar(100) null,
Place Varchar(100) null,
AreadID Uniqueidentifier null,
Pincode Varchar(100) null,

Area 
AreaID Primary key Uniqueidentifier not null,
DisplayName Varchar(100) null,
PrintName Varchar(100) null,
CityID Uniqueidentifier null.

Salutation
SalutationID Primary key Uniqueidentifier not null,
DisplayName Varchar(100) null,
PrintName Varchar(100) null

City
CityID Primary key Uniqueidentifier not null,
DisplayName Varchar(100) null,
PrintName Varchar(100) null , 
StateID Uniqueidentifier null

State
StateId Primary key Uniqueidentifier not null,
DisplayName Varchar(100) null  , 
PrintName Varchar(100) null , 
CountryID Uniqueidentifier null

Country
CountryID Primary key Uniqueidentifier not null,
DisplayName Varchar(100) null , 
PrintName Varchar(100) null ,



I completely Design the Database Design. Next I start to design the Form in Visiual Studio 2012 Express For web.
1) I connected the DB with Visiual Studio 2012 Express For web.
2)I created the EDMX File
3) I created the Model.

My Model is

Quote:

Quote:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Sample_Customer.Models
{
public class CustomerModel
{
public class Customer
{
public System.Guid CustomerID { get; set; }
public Nullable<system.guid> AccountsLedgerID { get; set; }
public string DisplayName { get; set; }
public string PrintName { get; set; }
public Nullable<system.guid> CustomerTypeID { get; set; }
}
public partial class CustomerType
{
public System.Guid CustomerTypeID { get; set; }
public string DisplayName { get; set; }
public string PrintName { get; set; }
public Nullable<system.guid> UnderCustomerTypeID { get; set; }
}
public partial class CustomerAddress
{
public System.Guid CustomerAddressID { get; set; }
public Nullable<system.guid> CustomerID { get; set; }
public Nullable<system.guid> AddressID { get; set; }
}
public partial class AddressType
{
public System.Guid AddressTypeID { get; set; }
public string DisplayName { get; set; }
public string PrintName { get; set; }
}

public partial class Address
{
public System.Guid AddressID { get; set; }
public Nullable<system.guid> AddressTypeID { get; set; }
public string DisplayName { get; set; }
public Nullable<system.guid> SalutationID { get; set; }
public string PrintName { get; set; }
public string Street { get; set; }
public string Location { get; set; }
public string Place { get; set; }
public Nullable<system.guid> AreaID { get; set; }
public string PinCode { get; set; }
}public partial class Salutation
{
public System.Guid SalutationID { get; set; }
public string DisplayName { get; set; }
public string PrintName { get; set; }
}public partial class Area
{
public System.Guid AreaID { get; set; }
public string DisplayName { get; set; }
public string PrintName { get; set; }
public Nullable<system.guid> CityID { get; set; }
}public partial class City
{
public System.Guid CityID { get; set; }
public string DisplayName { get; set; }
public string PrintName { get; set; }
public Nullable<system.guid> StateID { get; set; }
}
public partial class State
{
public System.Guid StateID { get; set; }
public string DisplayName { get; set; }
public string PrintName { get; set; }
public Nullable<system.guid>CountryID { get; set; }
}public partial class Country
{
public System.Guid CountryID { get; set; }
public string DisplayName { get; set; }
public string PrintName { get; set; }
}



Then I created the ViewModel To bring all these Fields In Single Form

Quote:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace Sample_Customer.Models
{
public class CustomerViewModel
{
public System.Guid CustomerID { get; set; }
public string CustomerName { get; set; }
public System.Guid CustomerTypeID { get; set; }
public string CustomerType { get; set; }
public string AddressTypeID{get;set ;
public string SalutationID { get; set; }
public string Street { get; set; }
public string Location { get; set; }
public string Place { get; set; }
public string AreaID { get; set; }
public string CityID { get; set; }
public string StateID { get; set; }
public string CountryID { get; set; }
public string PinCode { get; set; }

then I created the DBContect
public class VisitorsEntities : DbContext
{
public DbSet<customer> Customer1 { get; set; }
public DbSet<customertype> CustomerType { get; set; }
public DbSet<customeraddress> CustomerAddress { get; set; }
public DbSet

Address { get; set; }
public DbSet<addresstype> AddressType { get; set; }
public DbSet<salutation> Salutation { get; set; }
public DbSet<Area> Area { get; set; }
public DbSet<city> City { get; set; }
public DbSet<state> State { get; set; }
public DbSet<country> Country { get; set; }

}
}
}



Then I created the Controller Depend upon the ViewModel.
My Controller is

Quote:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sample_Customer.Models;

namespace Sample_Customer.Controllers
{
public class CustomerController : Controller
{
VisitorsEntities db = new VisitorsEntities();

// GET: /Customer/

public ActionResult Index()
{
return View();
}
public ActionResult Customer()
{
return View();
}
[HttpPost]
public ActionResult Customer(CustomerViewModel viewmodel)
{
var Customer = new Customer()
{
CustomerID= Guid .NewGuid (),
DisplayName = viewmodel.CustomerName,
PrintName = viewmodel .CustomerName
};

var CustomerType = new CustomerType()
{
DisplayName= viewmodel.CustomerType,
PrintName= viewmodel.CustomerType
};
var Salutation = new Salutation()
{
DisplayName = viewmodel .Salutation,
PrintName = viewmodel .Salutation.
}
var Area = new Area()
{
DisplayName = viewmodel.Area,
PrintName = viewmodel.Area
};

var City = new City()
{
DisplayName = viewmodel.City,
PrintName = viewmodel.City,
};
var State = new State()
{
DisplayName = viewmodel.State,
PrintName = viewmodel.State
};
var Country = new Country()
{
DisplayName = viewmodel.Country,
PrintName = viewmodel.Country
};
var Address = new Address()
{
//AddressTypeID = viewmodel .AddressType.ToString,
//SalutationID =viewmodel .Alias.ToString

DisplayName=viewmodel.CustomerName,
PrintName = viewmodel.CustomerName,
Street = viewmodel .Street,
Location = viewmodel.Location,
Place = viewmodel.Place,
};
var AddressType = new AddressType()
{
DisplayName= viewmodel .AddressType,
PrintName = viewmodel .AddressType
};
db.Customer.Add(Customer);
db.CustomerType.Add(CustomerType);
db.Address.Add(Address);
db.AddressType.Add(AddressType);
db.Salutation.Add(Salutation);
db.Area.Add(Area);
db.City.Add(City);
db.State.Add(State);
db.Country.Add(Country);

return View();
}
}
}




Here only My problem get started. Upto Model I created Correct. The Problem is ViewModel and Controller.
My Requirement is I want to bring all these Field in Single Form. That is
MainForm

CustomerID,CustomerName,NoofPartners,Salutation,Street,Place,Location,Area,City,State,Country,Pincode.

Sub Form
Area Form
Area
PrintName
City(Dropdown )

CityForm
CityName
State(DropDown)

State Form
StateName
Country(DropDown)

Country Form
CountryName

As like this I want.CustomerType, Area, City, State, Country all are separate forms.And the Address Type is 3 radio buttons which has to keep in the Main Form.And I just want to Refer that each Form ID to main Form.For that how can i create the View Model and Controller. When I click the save button It have to save the data in multiple tables.I tried my level best to explain my question detaily.

Thanks

解决方案

Have a look at this one for creating partial view

Partial View in ASP.NET MVC 4[^]


These links for CRUD operations



Complete CRUD Operations in MVC 4 using Entity Framework 5 Without Writing a Single Line of Code[^]

CRUD Operations Easy with New Scaffolding Feature of MVC 5 using VS2013[^]


这篇关于使用MVC4中的单一视图将数据插入多个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆