//save to JSON file protected void saveBtn_Click(object sender, EventArgs e) { _Model.name = nameTxt.Text; _Model.faction = factionTxt.Text; _Model.rank = Convert.ToInt32(rankTxt.Text); _Model._base = Convert.ToInt32(baseTxt.Text); _Model.size = Convert.ToInt32(sizeTxt.Text.Remove(sizeTxt.Text.Length - 2, 2)); // remove "mm" from textbox to avoid exception _Model.deploymentZone = deploymentZoneTxt.Text; _Model.mobility = Convert.ToInt32(mobilityTxt.Text); _Model.willpower = Convert.ToInt32(willpowerTxt.Text); _Model.resiliance = Convert.ToInt32(resilianceTxt.Text); _Model.wounds = Convert.ToInt32(woundsTxt.Text); // declare a TextBox since texboxes from repeater are not accessible TextBox specialName = new TextBox(); TextBox specialDesc = new TextBox(); TextBox actionName = new TextBox(); TextBox actionType = new TextBox(); TextBox actionRating = new TextBox(); TextBox actionRange = new TextBox(); TextBox actionDescription = new TextBox(); TextBox[] traits = new TextBox[traitsRepeater.Items.Count]; TextBox[] types = new TextBox[typesRepeater.Items.Count]; TextBox[] defenseChartItems = new TextBox[defenseChartRepeater.Items.Count]; Models.Action[] actions = null; Specialability[] specialAbilities = null; if (_Model.specialAbilities == null) { specialAbilitiesRepeater.Visible = false; } else { specialAbilities = new Specialability[_Model.specialAbilities.Length]; } if (_Model.actions == null) { actionsRepeater.Visible = false; } else { actions = new Models.Action[_Model.actions.Length]; } // loop trough each item of the special abilities repeater foreach (RepeaterItem item in specialAbilitiesRepeater.Items) { specialName = (TextBox)item.FindControl("specialAbName"); specialDesc = (TextBox)item.FindControl("specialAbDesc"); specialAbilities[item.ItemIndex] = new Specialability { name = specialName.Text, description = specialDesc.Text }; } _Model.specialAbilities = specialAbilities; // loop trough each item of the actions repeater foreach (RepeaterItem item in actionsRepeater.Items) { actionName = (TextBox)item.FindControl("actionName"); actionType = (TextBox)item.FindControl("actionType"); actionRating = (TextBox)item.FindControl("actionRating"); actionRange = (TextBox)item.FindControl("actionRange"); actionDescription = (TextBox)item.FindControl("actionDescription"); actions[item.ItemIndex] = new Models.Action { name = actionName.Text, type = actionType.Text, rating = Convert.ToInt32(actionRating.Text), range = actionRange.Text, description = actionDescription.Text }; } _Model.actions = actions; // assign values of textBoxes foreach (RepeaterItem item in traitsRepeater.Items) { traits[item.ItemIndex] = (TextBox)item.FindControl("trait"); } // add traits to the traits array for (int i = 0; i < traitsRepeater.Items.Count; i++) { _Model.traits[i] = traits[i].Text; } // assign values of textBoxes foreach (RepeaterItem item in typesRepeater.Items) { types[item.ItemIndex] = (TextBox)item.FindControl("type"); } // add traits to the traits array for (int i = 0; i < typesRepeater.Items.Count; i++) { _Model.types[i] = types[i].Text; } // add textboxes of the repeater to the textbox array foreach (RepeaterItem item in defenseChartRepeater.Items) { defenseChartItems[item.ItemIndex] = (TextBox)item.FindControl("defenseChartItem"); } // add defense item to the appropriate index in the defenseChartArray array for (int i = 0; i < defenseChartRepeater.Items.Count; i++) { _Model.defenseChart[i] = defenseChartItems[i].Text; } // update the new JSON file and redirect back to model page Global.UpdateModelCollection(); Response.Redirect(string.Format("SingleModel.aspx?name={0}&faction={1}", name, faction)); }
/// <summary> /// Read all the validated input and transforms the data entered by the user into a model to be put on the list. /// </summary> /// <returns>The built model.</returns> private Mini BuildModelFromInputs() { // Action fields. Models.Action action = new Models.Action(); action.Name = ModelActionNameTextBox.Text; action.Type = ModelActionTypeTextBox.Text; action.Rating = uint.Parse(ModelActionRatingTextBox.Text); action.Range = ModelActionRangeTextBox.Text; if (!string.IsNullOrWhiteSpace(ModelActionDescriptionTextBox.Text)) { action.Description = ModelActionDescriptionTextBox.Text; } // Special Ability fields. SpecialAbility specialAbility = null; if (!string.IsNullOrWhiteSpace(ModelSpecialAbilityNameTextBox.Text)) { specialAbility = new SpecialAbility(); specialAbility.Name = ModelSpecialAbilityNameTextBox.Text; if (!string.IsNullOrWhiteSpace(ModelSpecialAbilityDescriptionTextBox.Text)) { specialAbility.Description = ModelSpecialAbilityDescriptionTextBox.Text; } } // Mini fields. Mini mini = new Mini(); mini.Name = ModelNameTextBox.Text; mini.Faction = ModelFactionTextBox.Text; mini.Rank = uint.Parse(ModelRankTextBox.Text); mini.Base = uint.Parse(ModelBaseTextBox.Text); mini.Size = uint.Parse(ModelSizeTextBox.Text); mini.DeploymentZone = ModelDeploymentZoneTextBox.Text; mini.Mobility = uint.Parse(ModelMobilityTextBox.Text); mini.Willpower = uint.Parse(ModelWillPowerTextBox.Text); mini.Resiliance = uint.Parse(ModelResilianceTextBox.Text); mini.Wounds = uint.Parse(ModelWoundsTextBox.Text); mini.Types = new List <string>(ModelTypesTextBox.Text.Split(Mini.SEPARATOR)); mini.DefenseChart = new List <string>(ModelDefenceChartTextBox.Text.Split(Mini.SEPARATOR)); mini.ImageUrl = ModelImageUrlTextBox.Text; mini.Actions = new List <Models.Action> { action }; if (null != specialAbility) { mini.SpecialAbilities = new List <SpecialAbility> { specialAbility }; } if (!string.IsNullOrWhiteSpace(ModelTraitsTextBox.Text)) { mini.Traits = new List <string>(ModelTraitsTextBox.Text.Split(Mini.SEPARATOR)); } return(mini); }