Sneha K

Sneha K

  • 1.2k
  • 527
  • 195.5k

The Guid Is not passed Correctly in MVC4?

Dec 17 2015 7:53 AM

Hi the GUID is not passed correctly.

My Tables

Customer

CustomerID uniqueidentifier primarykey not null,

DisplayName varchar(100)

PrintName varchar(100)

CustomerAddress Table

CustomerAddressID uniqueidentifier primarykey not null,

CustomerID uniqueidentifier null,

AddressID uniqueidentifier null

Address

AddressID uniqueidentifier primarykey not null,

DisplayName varchar(100)

Street varchar(100)

Location varchar(100)

AreaID Uniqueidentifier null,

Pincode varchar(100)

My Model
 
 
public class CustomerModel1
{
public System.Guid CustomerID { get; set; }
public string DisplayName { get; set; }
public string PrintName { get; set; }
}
public partial class CustomerAddressModel
{
public System.Guid CustomerAddressID { get; set; }
public Nullable<System.Guid> CustomerID { get; set; }
public Nullable<System.Guid> AddressID { get; set; }
}
public partial class AddressModel
{
public System.Guid AddressID { get; set; }
public string DisplayName { get; set; }
public string Street { get; set; }
public string Location { get; set; }
public Nullable<System.Guid> AreaID { get; set; }
public string PinCode { get; set; }
}
 
My ViewModel 
 
 
public class CustomerViewModel
{
public System.Guid CustomerID { get; set; }
public string CustomerName { get; set; }
public System.Guid CustomerAddressID { get; set; }
public System.Guid AddressID { get; set; }
public string Address { get; set; }
public string Street { get; set; }
public string Location { get; set; }
public Nullable<System.Guid> AreaID { get; set; }
public string PinCode { get; set; }
}
public class VisitorsEntities1 : DbContext
{
public DbSet Customer { get; set; }
public DbSet CustomerAddress { get; set; }
public DbSet Address { get; set; }
}
 
My Controller
 
 
public ActionResult Create()
{
VisitorsEntities1 db = new VisitorsEntities1();
ViewBag.AreaID = new SelectList(db.Area, "AreaID", "DisplayName", "PrintName");
return View();
}
[HttpPost]
public ActionResult Create(CustomerViewModel viewmodel)
{
ViewBag.AreaID = new SelectList(db.Area, "AreaID", "DisplayName", viewmodel.AreaID);
var Cusobj = Guid.NewGuid();
var CusAddobj = Guid.NewGuid();
var Addobj = Guid.NewGuid();
var Customerobj = new Customer()
{
CustomerID = Cusobj,
DisplayName = viewmodel.CustomerName,
PrintName = viewmodel .CustomerName
};
var CustomerAddressobj = new CustomerAddress()
{
CustomerAddressID = CusAddobj,
CustomerID = viewmodel.CustomerID,
AddressID = viewmodel.AddressID
};
var Addressobj = new Address()
{
AddressID = Addobj,
DisplayName= viewmodel.CustomerName,
Street=viewmodel.Street,
Location= viewmodel.Location,
AreaID = viewmodel.AreaID,
PinCode= viewmodel.PinCode
};
 
db.Customer.Add(Customerobj);
db .CustomerAddress.Add(CustomerAddressobj );
db.Address.Add(Addressobj);
 
 
 
db.SaveChanges();
return View()
}
Here the AreaID is SubForm So I used ViewBag to bring dropdown in the View and Pass the Guid. Now my issue is with Customer Address Table.
 
 
var CustomerAddressobj = new CustomerAddress()
{
CustomerAddressID = CusAddobj,
CustomerID = viewmodel.CustomerID,
AddressID = viewmodel.AddressID
};
Here CustomerAddressID is new Guid is working fine. But
CustomerID = viewmodel.CustomerID,
AddressID = viewmodel.AddressID
 
 The Guid is not passed correctly for this two lines . In table these column is 00000-0000-0000-0000 It didnt show any error and also its not passing the GUID correctly. I donno what the issue is. Please any one help to resolve these problem?
 
 Error
 
 
Thanks. 
 

Answers (4)