Click here to Skip to main content
15,921,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
(CustomerID = viewmodel.CustomerID,
AddressID = viewmodel.AddressID)
.

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?
Posted
Updated 17-Dec-15 2:22am
v3
Comments
Nathan Minier 17-Dec-15 8:28am    
Do you have hidden fields on the form associated for those items? If there is no form entry in MVC, it will not pass that properly back up to the controller.

Like:
@Html.HiddenFor(model => model.CustomerID);
@Html.HiddenFor(model => model.AddressID);
Member 12087373 17-Dec-15 8:33am    
Yes I hidden those two fields in the View

1 solution

Please use already created Guid for CustomerAddressObj object.

Use below code :
var CustomerAddressobj = new CustomerAddress()
{
CustomerAddressID = CusAddobj,
CustomerID = Cusobj,
AddressID = Addobj
};

Hopefully it will work ...!
 
Share this answer
 

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