Exemplo n.º 1
0
        private void SaveAttributes(HttpRequestBase req, int productID)
        {
            var exists = false;

            foreach (string key in req.Form)
            {
                if (key.StartsWith("attr_"))
                {
                    exists = true;
                    break;
                }
            }

            if (!exists)
            {
                return;
            }

            AttributeValues.DeleteBy(productID);

            var attrValues = new List <AttributeValue>();

            foreach (string key in req.Form)
            {
                if (key.StartsWith("attr_"))
                {
                    var id    = int.Parse(key.Replace("attr_", String.Empty));
                    var value = req.Form[key];

                    var attr      = Attributes.GetByID(id);
                    var attrValue = new AttributeValue();

                    attrValue.ProductID   = productID;
                    attrValue.AttributeID = attr.ID;

                    attrValues.Add(attrValue);

                    switch (attr.AttributeType)
                    {
                    case AttributeType.Text:
                        attrValue.Value = value;
                        break;

                    case AttributeType.Number:
                        attrValue.Value = value;
                        break;

                    case AttributeType.SingleItem:
                        if (!String.IsNullOrWhiteSpace(value))
                        {
                            attrValue.AttributeOptionID = int.Parse(value);
                        }
                        break;

                    case AttributeType.MultipleItem:
                        attrValues.Remove(attrValues.Last());

                        var selecteds = value.Split(',');
                        foreach (var item in selecteds)
                        {
                            var atv = (AttributeValue)attrValue.Clone();
                            atv.AttributeOptionID = int.Parse(item);

                            attrValues.Add(atv);
                        }

                        break;

                    case AttributeType.Check:
                        //attrValue.Value = bool.Parse(value).ToString();
                        break;

                    case AttributeType.MultilineText:
                        attrValue.Value = value;
                        break;

                    default:
                        break;
                    }
                }
            }

            foreach (var item in attrValues)
            {
                AttributeValues.Insert(item);
            }
        }