2016-08-29 71 views
0

將動態生成的文本框輸入傳遞給MVC5控制器的最佳方法是什麼?向控制器發佈動態生成的模型列表

我想存檔這個問題,但我卡住了。我正在從Web.config文件中讀取數據,並將該數據綁定到我的模型,該模型被稱爲Demand併發送到索引以顯示給用戶。用戶將更改輸入並重新發送到控制器以更改AppSettings。我的問題是如何從使用MVC5功能(如Html.TextBoxFor)獲得用戶的數據,但我不明白我該如何實現自己。我試圖將數據推送到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

+0

您可以使用'foreach'循環爲收集項目生成表單控件。你需要使用'for'循環或者(更好)一個自定義'EditorTemplate'來輸入'Demand'類型 - 參考[this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado- net-datatable/30094943#30094943) –

+0

我設法發送數據。謝謝 –

回答

0

感謝斯蒂芬·馬克。我成功地將數據發送給控制器。你可以查看評論中的鏈接。