0

What is the best way to pass dynamically generated textbox inputs to controller for MVC5?

I'm trying to archive this problem but I'm stuck. I'm reading my data from Web.config file and binding that data to my Model which is calling as Demand and sending to index to show for the user. User will change the inputs and repost to the controller to change the AppSettings. My problem is how can I get the data from the user with using MVC5 capabilities like Html.TextBoxFor but I didn't understand how can I implement to myself. I'm trying to push the data to Controller's ChangeConfig function. If someone help me to the index and explain what s/he did, i will be glad.

Web.Config file:

<add key="DefaultDemands" value="Always:ASG_Live_XXXXX:-1:4:13:-1:6:13:-1:2:13:-1:2:13;Always:ASG_Live_YYYY:-1:4:27:-1:12:27:-1:2:27:-1:2:27 />

Model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace InstanceScheduler.Models
{
    public class Demand
    {
        public string AutoScalingGroupName { get; set; }

        public string Type { get; set; }

        public int Normal_ScaleUP_DesiredCapacity { get; set; }
        public int Normal_ScaleUP_MinSize { get; set; }
        public int Normal_ScaleUP_MaxSize { get; set; }
        public int Derby_ScaleUP_DesiredCapacity { get; set; }
        public int Derby_ScaleUP_MinSize { get; set; }
        public int Derby_ScaleUP_MaxSize { get; set; }

        public int Normal_ScaleDOWN_DesiredCapacity { get; set; }
        public int Normal_ScaleDOWN_MinSize { get; set; }
        public int Normal_ScaleDOWN_MaxSize { get; set; }
        public int Derby_ScaleDOWN_DesiredCapacity { get; set; }
        public int Derby_ScaleDOWN_MinSize { get; set; }
        public int Derby_ScaleDOWN_MaxSize { get; set; }
    }
}

Controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.Configuration;
using InstanceScheduler.Models;

namespace InstanceScheduler.Controllers
{
    public class AdminController : Controller
    {
        // GET: Admin
        public ActionResult Index()
        {
            string _configDemandsString = ConfigurationManager.AppSettings["DefaultDemands"];
            List<Demand> _demandList = new List<Demand>();
            foreach (var _demandString in _configDemandsString.Split(';'))
            {
                var _parseDemandString = _demandString.Split(':');
                _demandList.Add(new Demand
                {
                    Type = _parseDemandString[0],
                    AutoScalingGroupName = _parseDemandString[1],
                    Normal_ScaleUP_DesiredCapacity = int.Parse(_parseDemandString[2]),
                    Normal_ScaleUP_MinSize = int.Parse(_parseDemandString[3]),
                    Normal_ScaleUP_MaxSize = int.Parse(_parseDemandString[4]),
                    Derby_ScaleUP_DesiredCapacity = int.Parse(_parseDemandString[5]),
                    Derby_ScaleUP_MinSize = int.Parse(_parseDemandString[6]),
                    Derby_ScaleUP_MaxSize = int.Parse(_parseDemandString[7]),
                    Normal_ScaleDOWN_DesiredCapacity = int.Parse(_parseDemandString[8]),
                    Normal_ScaleDOWN_MinSize = int.Parse(_parseDemandString[9]),
                    Normal_ScaleDOWN_MaxSize = int.Parse(_parseDemandString[10]),
                    Derby_ScaleDOWN_DesiredCapacity = int.Parse(_parseDemandString[11]),
                    Derby_ScaleDOWN_MinSize = int.Parse(_parseDemandString[12]),
                    Derby_ScaleDOWN_MaxSize = int.Parse(_parseDemandString[13])
                });
            }

            return View(_demandList);
        }

        public string ChangeConfig(List<Demand> _demands)
        {
            return string.Join(",", _demands.Select(q => q.AutoScalingGroupName));
        }
    }
}

Index View Code:

@model List<InstanceScheduler.Models.Demand>

<div class="container-fluid">
    <form action="@Url.Action("ChangeConfig","Admin")">
        @{
            foreach (var _demand in Model)
            {
                foreach (var _variable in new InstanceScheduler.Models.Demand().GetType().GetProperties())
                {
                    <div class="row">
                        @_variable.Name: <input value="@typeof(InstanceScheduler.Models.Demand).GetProperty(_variable.Name).GetValue(_demand)" />
                    </div>
                }
                <br />
                <br />
            }
        }
        <button class="btn btn-primary" type="submit">Submit</button>
    </form>
</div>

Index Screenshot:

Index Screenshot

ekad
  • 14,436
  • 26
  • 44
  • 46
  • You canot use `foreach` loops to generate form controls for collection items. You need to use a `for` loop or (better) a custom `EditorTemplate` for typeof `Demand` - refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) –  Aug 29 '16 at 12:05
  • I managed to send the data. Thank you – Kerim Doruk Akıncı Aug 29 '16 at 12:22

1 Answers1

0

Thanks to Stephen Muecke. I succeed sending the data to controller. you can check the link in the comment.