发布动态生成的模型列表到控制器

时间:2016-08-29 12:01:39

标签: asp.net-mvc razor asp.net-mvc-5

将动态生成的文本框输入传递给MVC5控制器的最佳方法是什么?

我正在尝试归档这个问题但是我被困住了。我正在从Web.config文件中读取数据并将该数据绑定到我的模型,该模型正在调用为Demand并发送到索引以显示给用户。用户将更改输入并重新发布到控制器以更改AppSettings。我的问题是如何使用像Html.TextBoxFor这样的MVC5功能从用户那里获取数据,但我不明白我该如何实现自己。我正在尝试将数据推送到Controller的ChangeConfig函数。如果有人帮我索引并解释他/她做了什么,我会很高兴。

Web.Config文件:

<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 />

型号:

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; }
    }
}

控制器:

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));
        }
    }
}

索引查看代码:

@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

1 个答案:

答案 0 :(得分:0)

感谢Stephen Muecke。我成功将数据发送到控制器。你可以查看评论中的链接。