Sneha K

Sneha K

  • 1.2k
  • 527
  • 195.2k

How to insert "Multiple Field Value"in Single Column in MVC4

Dec 2 2015 11:58 PM

Please leave  reading the codings part. I mentioned my issue in last. Why i include my DB and Codings means suppose if you didn't understand my issue you can refer this (i.e) what i did from the Starting Itself.

In my project i have Customer Master. It contain 16 fields like

1) Customer ID2) Customer Name 3) Alias 4) Customer Type 5) Street 6) Location 7) Area 8) City 9) Stat 10) Country 11 Tin No 12 Cst No 13 Service Tax No 14 Excise Reg No 15 Pan No 16) Cin No

My CustomerMaster Tables are

Customer
CustomerID Primary key Uniqueidentifier not null,                                                                                       
Display Name Varchar(100) null,
Print Name Varchar(100) null,
CustomerType ID 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.

CustomerTaxInfo

CustomerTaxInfoID Primary key Uniqueidentifier not null,

CustomerID Uniqueidentifier null,
TaxInfoReference Varchar(100) null ,
TaxInfoID Uniqueidentifier null,

Taxfield TaxFieldID PrimaryKey Uniqueidentifier not null,
DisplayName Varchar(100) null ,
PrintName Varchar(100) null.

TaxInfo

TaxInfoID PrimaryKey Uniqueidentifier not null,
DisplayName Varchar(100) null ,
PrintName Varchar(100) null.

TaxInfoTaxField

TaxInfoTaxFieldID PrimaryKey Uniqueidentifier not null,
TaxInfoID Uniqueidentifier null,,
TaxFieldID Uniqueidentifier null,
FieldValue big int,

Here CustomerType , Area, Salutation, City, State ,Country all are Separate View. I referred these Id's to Main View(Customer View) . If I connect my DB with Vb Express 2012 for Database Connection . Then I create the EDMX File. So it will automatically create Separate view for each Table. So I decided to bring all the Fields in Single View . So I Used “Code First approach” and “ViewModel”. I Created the Model and ViewModel .
 My ViewModel

 

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 SalutationID { get; set; }
public string Alias { get; set; }
public System.Guid CustomerTypeID { get; set; }
public string CustomerType { get; set; }
public System.Guid AddressTypeID { get; set; }
public string AddressType { get; set; }
public System.Guid CustomerAddressID { get; set; }
public System.Guid AddressID { 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 Area { get; set; }
public Nullable<System.Guid> CityID{ get; set; }
public string City { get; set; }
public Nullable<System.Guid> StateID{ get; set; }
public string State { get; set; }
public Nullable<System.Guid> CountryID{ get; set; }
public string Country { get; set; }
public string PinCode { get; set; }
public System.Guid TaxInfoID { get; set; }
public Nullable<System.Guid> TaxFiledID { get; set; }
public System.Guid CustomerTaxInfo { get; set; }
public string TinNo { get; set; }
public string CstNo { get; set; }
public string ExciseRegNo { get; set; }
public string PanNo { get; set; }
public string ServiceTaxNo { get; set; }
public string CinNo { get; set; }
}
public class VisitorsEntities1 : DbContext
{
public DbSet<Sample_Customer.Models.CustomerModel.CustomerModel1> Customer { get; set; }
public DbSet<Sample_Customer.Models.CustomerModel.CustomerTypeModel> CustomerType { get; set; }
public DbSet<Sample_Customer.Models.CustomerModel.CustomerAddressModel> CustomerAddress { get; set; }
public DbSet<Sample_Customer.Models.CustomerModel.AddressModel> Address { get; set; }
public DbSet<Sample_Customer.Models.CustomerModel.AddressTypeModel> AddressType { get; set; }
public DbSet<Sample_Customer.Models.CustomerModel.SalutationModel> Salutation { get; set; }
public DbSet<Sample_Customer.Models.CustomerModel.AreaModel>Area{ get; set; }
public DbSet<Sample_Customer.Models.CustomerModel.CityModel> City { get; set; }
public DbSet<Sample_Customer.Models.CustomerModel.StateModel> State { get; set; }
public DbSet<Sample_Customer.Models.CustomerModel.CountryModel> Country { get; set; }
public DbSet<Sample_Customer.Models.CustomerModel.CustomerContactModel> CustomerContact { get; set; }
public DbSet<Sample_Customer.Models.CustomerModel.ContactModel> Contact { get; set; }
public DbSet<Sample_Customer.Models.CustomerModel.ContactTypeModel> ContactType { get; set; }
public DbSet<Sample_Customer.Models.CustomerModel.CustomerTaxInfoModel> CustomerTaxInfo { get; set; }
public DbSet<Sample_Customer.Models.CustomerModel.TaxInfoModel> TaxInfo { get; set; }
public DbSet<Sample_Customer.Models.CustomerModel.TaxFieldModel> TaxField { get; set; }
public DbSet<Sample_Customer.Models.CustomerModel.TaxInfoTaxFiledModel> TaxInfoTaxFiled { get; set; }
}
}
 

And My Controller

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

{

VisitorsEntities1 db = new VisitorsEntities1();

//

// GET: /Customer/

public ActionResult Index()

{

return View();

}

public ActionResult Create()

{

VisitorsEntities1 db = new VisitorsEntities1();

ViewBag.Country = new SelectList(db.Country, "CountryID", "DisplayName","PrintName");

ViewBag.State = new SelectList(db.State, "StateID", "DisplayName", "PrintName");

ViewBag.City = new SelectList(db.City, "CityID", "DisplayName", "PrintName");

ViewBag.Area = new SelectList(db.Area, "AreaID", "DisplayName", "PrintName");

return View();

[HttpPost

public ActionResult Create(CustomerViewModel viewmodel) {

ViewBag.Country = new SelectList(db.Country, "CountryID", "DisplayName","PrintName");

ViewBag.State = new SelectList(db.State, "StateID", "DisplayName", "PrintName");

ViewBag.City = new SelectList(db.City, "CityID", "DisplayName", "PrintName");

ViewBag.Area = new SelectList(db.Area, "AreaID", "DisplayName", "PrintName");

var Customerobj = new Customer()

{

CustomerID= Guid .NewGuid (),

DisplayName = viewmodel.CustomerName,

PrintName = viewmodel .CustomerName,

CustomerType =viewmodel.CustomerTypeID,

};

var CustomerTypeobj = new CustomerType()

{

CustomerTypeID= Guid.NewGuid(),

DisplayName = viewmodel.CustomerType,

PrintName= viewmodel.CustomerType

};

var CustomerAddressobj = new CustomerAddress()

{

CustomerAddressID= Guid.NewGuid(),

CustomerID= viewmodel.CustomerID,

AddressID= viewmodel.AddressID

};

var Addressobj = new Address()

{

AddressID = Guid.NewGuid(),

AddressTypeID = viewmodel.AddressTypeID,

DisplayName= viewmodel.CustomerName,

Salutation= viewmodel.Alias,

PrintName= viewmodel.CustomerName,

Street=viewmodel.Street,

Location= viewmodel.Location,

Place= viewmodel.Place,

Area = viewmodel.AreaID,

PinCode= viewmodel.PinCode

};

 

while Saving the data in Database using Code First approach is working fine upto Address.That is It saving the data up to Address details in DB is working Fine. But I got a problem while Saving the Tax details.In my view I have Six Fields like TinNo , CstNo, PanNo, ExciseRegNo, ServiceTxNo, CinNo.Each one have Default value which is saved in TaxField Table.

 

Eg TinNo = FD713788-B5AE-49FF-8B2C-F311B9CB0CC4

CstNo = 64B512E7-46AE-4989-A049-A446118099C4

CinNo =59A2449A-C5C6-45B5-AA00-F535D83AD48B

Same as like that other field PanNo, ExciseRegNo, ServiceTxNo contain Default Value .

Suppose now I am going to save one customer Details in CustomerMaster view and enter the TinNo ,CstNo, PanNo, ExciseRegNo, ServiceTxNo, CinNo, means it have to save the values(Which is entered in the Field of TinNo in the View) in FieldValue and their ID ( eg TinNo = FD713788-B5AE-49FF-8B2C-F311B9CB0CC4) in TaxFieldID of TaxInfoTaxField Table.That is I need to save those six Field in same column not in same row and same cell. Suppose if I enter the TinNo means it have to save the value in FieldValue and ID in TaxFieldID. Then I enter the CstNo it will automatically go and create the new row and have to save the Value and ID in the FieldValue and TaxFieldID. So for each Customer it will create 6 rows automatically while Saving TaxDetails. How to do this MVC4 ? 

 
Thanks.. 


 
 
 
 

Answers (7)