Edit Associated Profile Entities

Create a page using addresses within an MVC site.

Complete the following prerequisites:

At your visual studio solution setup:

  1. Get all user addresses
    List<MVCSite.Models.Generated.Address> models = null;
    if (Sitecore.Context.User.IsAuthenticated)
    {
    	var user = Sitecore.Context.User;
    	var commerceProfile = user.GetCommerceProfileModel();
    
    	if (commerceProfile.AddressList != null)
    	{
    		models = Address.GetMultiple(commerceProfile.AddressList);
    	}
    }
    return View(models);
    
  2. Create or edit an address.
    if (model != null && Sitecore.Context.User.IsAuthenticated)
    {
    	var user = Sitecore.Context.User;
    	var profileModel = user.GetCommerceProfileModel();
    
    	if (string.IsNullOrEmpty(model.AddressID))
    	{
    		var address = Address.Create();
    
    		model.Save(address);
    
    		profileModel.AddressList.Add(model.AddressID);
    		profileModel.AddressList = profileModel.AddressList;
    		profileModel.Save();
    	}
    	else
    	{
    		if (profileModel.AddressList.Contains(model.AddressID))
    		{
    			var address = Address.GetCommerceProfile(model.AddressID);
    			if (address != null)
    			{
    				model.Save(address);
    			}
    		}
    	}
    }