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