Exemplo n.º 1
0
 public ActionResult Edit([Bind(Include = "KeyValueId,KeyTypeId,Key,Value,Description,StartDate,EndDate,IsActive")] KeyValueView instance)
 {
     if (ModelState.IsValid)
     {
         var keyValue = UnitOfWork.KeyManager.GetKeyValue(instance.KeyValueId);
         keyValue.Description = instance.Description;
         keyValue.EndDate     = instance.EndDate;
         keyValue.Key         = instance.Key;
         keyValue.KeyTypeId   = instance.KeyTypeId;
         keyValue.StartDate   = instance.StartDate;
         keyValue.Value       = instance.Value;
         UnitOfWork.KeyManager.UpdateKeyValue(keyValue);
         return(PartialView(keyValue));
     }
     ViewBag.KeyTypeId = new SelectList(UnitOfWork.KeyManager.GetKeyTypes(), "KeyTypeId", "Description", instance.KeyTypeId);
     return(PartialView(instance));
 }
Exemplo n.º 2
0
        private async Task <Grid> AddWidgetsAsync(Grid grid, JObject layoutObject)
        {
            var widgets       = (JArray)layoutObject["widgets"];
            var widgetObjects = widgets
                                .OfType <JObject>()
                                .Select((widget) =>
            {
                return(new
                {
                    Name = widget.Value <string>("name"),
                    Key = widget.Value <string>("key"),
                    Column = widget.Value <int>("column"),
                    Row = widget.Value <int>("row"),
                    RowSpan = widget.Value <int?>("rowSpan"),
                    WidgetView = widget.Value <string>("view"),
                    Type = widget.Value <string>("type"),
                    DefaultValue = widget.Value <string>("defaultValue"),
                    DefaultModifierValue = widget.Value <string>("defaultModifierValue"),
                    DefaultScoreValue = widget.Value <string>("defaultScoreValue"),
                    DefaultSaveValue = widget.Value <string>("defaultSaveValue")
                });
            })
                                .ToList();

            var widgetKeys  = widgetObjects.Select(w => w.Key).ToList();
            var abilityKeys = widgetObjects
                              .Where(w => w.Type == "abilityScore")
                              .SelectMany((w) => new string[] {
                $"{w.Key}-modifier",
                $"{w.Key}-score",
                $"{w.Key}-save",
            });

            widgetKeys.AddRange(abilityKeys);

            Dictionary <string, string> keyValues;

            using (var keyValueContext = new KeyValueContext())
            {
                keyValues = await keyValueContext.KeyValues
                            .Where(kv => widgetKeys.Contains(kv.Key))
                            .ToDictionaryAsync((kv) => kv.Key, (kv) => kv.Value);
            }

            foreach (var widgetObject in widgetObjects)
            {
                View view;
                switch (widgetObject.WidgetView)
                {
                case "editLabel":
                    view = new EditLabelView(this)
                    {
                        LabelTitle = widgetObject.Name,
                        LabelKey   = widgetObject.Key,
                        LabelValue = keyValues.ContainsKey(widgetObject.Key) ? keyValues[widgetObject.Key] : widgetObject.DefaultValue
                    };
                    break;

                case "keyValue":
                    view = new KeyValueView(this)
                    {
                        BoxTitle = widgetObject.Name,
                        BoxKey   = widgetObject.Key,
                        BoxValue = keyValues.ContainsKey(widgetObject.Key) ? keyValues[widgetObject.Key] : widgetObject.DefaultValue
                    };
                    break;

                case "abilityScore":
                    var modifierKey = $"{widgetObject.Key}-modifier";
                    var saveKey     = $"{widgetObject.Key}-save";
                    var scoreKey    = $"{widgetObject.Key}-score";
                    view = new AbilityScoreView(this)
                    {
                        AbilityName          = widgetObject.Name,
                        AbilityKey           = widgetObject.Key,
                        AbilityModifierValue = keyValues.ContainsKey(modifierKey) ? keyValues[modifierKey] : widgetObject.DefaultModifierValue,
                        AbilitySaveValue     = keyValues.ContainsKey(saveKey) ? keyValues[saveKey] : widgetObject.DefaultSaveValue,
                        AbilityScoreValue    = keyValues.ContainsKey(scoreKey) ? keyValues[scoreKey] : widgetObject.DefaultScoreValue,
                    };
                    break;

                default:
                    continue;
                }

                Grid.SetColumn(view, widgetObject.Column);
                Grid.SetRow(view, widgetObject.Row);
                if (widgetObject.RowSpan != null)
                {
                    Grid.SetRowSpan(view, widgetObject.RowSpan.Value);
                }
                grid.Children.Add(view);
            }

            return(grid);
        }