Click here to Skip to main content
15,936,119 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
SQL
<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
Posted
Updated 4-Nov-15 19:34pm
v8
Comments
John C Rayan 5-Nov-15 5:23am    
Are you saying that all these forms into a single form?
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
sewtha22 5-Nov-15 5:46am    
Yes John . I want to bring CustomerID(TextBox) ,CustomerName(TextBox) , NoOf partners(TextBox), Salutation(DropDown),Street(TextBox),Place(TextBox),Loction(TextBox), Area(DropDown), City (DropDown),State(DropDown) , Country(DropDown),Pincode(TextBox) in Single View.

Area is Separate Form that is
Area Form Contain Area, Display Name, PrintName, City ID.
Now I am opening the Customer Form and Add the New Customer Means I will Enter the CustomerID, CustomerName NoOfPartners,Street, Place,Location,Pincode. and need to select the Dropdown of Salutation, Area, City, State, Country, are Dropdown. Suppose If i need add the new Country State City Area Means i need to go for State View or city (that view ) and add the names. In main Form Area, City, Country, state are Dropdown .I refer the AreaForm ID to the main Form.. How can i do this
John C Rayan 5-Nov-15 6:15am    
Have you tried Partial View ?
sewtha22 5-Nov-15 7:06am    
No actually I want a single view because my requirement is Single View john
John C Rayan 5-Nov-15 7:16am    
When you say Area is Separate Form that is
Area Form Contain Area, Display Name, PrintName, City ID.
Do you mean separate View or separate form within the single view?

So basically you would like to have a single view with multiple forms? what problem are you facing? what errors?

Can you post your view here?

Have a look at this one for creating partial view

Partial View in ASP.NET MVC 4[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900