【问题标题】:ASP.NET MVC, Bootstrap Tables, get values for each columnASP.NET MVC,引导表,获取每列的值
【发布时间】:2015-12-10 09:04:55
【问题描述】:

在 ASP.NET MVC 中,我有一个动作,它接受用户关于行和列的输入,然后导航到根据用户输入生成所需行数和列数的动作,如下所示:-

观看次数:

<div class="container" style='width:@(240 * Model.Columns.Capacity)px;overflow-y:auto'>
<div class="row">
    @using (Html.BeginForm())
    {
        <table style='width:100%' class='table table-condensed'>
            <tbody>
                @for (int k = 0; k < Model.Rows.Capacity; k++)
                {
                    <tr>
                        @for (int i = 0; i < Model.Columns.Capacity; i++)
                        {
                            <td>
                                @Html.TextBox("name" + k + i, null, new { @class = "form-control" })
                            </td>
                        }
                    </tr>
                }
            </tbody>
        </table>

        <input type="submit" name="name" value="Submit" class="btn btn-primary" />
    }
</div>

进入之后。

http://i.stack.imgur.com/lxhOM.png

在发布操作中,我有一个表单集合,以便使用表单集合在发布操作中获取填充数据。在发布操作中,我使用 foreach 循环获取每个输入字段的值。

    [HttpPost]
    public ActionResult Enter(FormCollection form)
    {
        for (int i = 0; i < form.Count; i++)
        {
            Debug.WriteLine(form[i]);
        }
        return View();
    }

我只想知道如何在 post 操作中获取单个列的所有值并逐列获取它们,默认情况下它获取水平的值。

谢谢。 http://i.stack.imgur.com/SBMvY.png

【问题讨论】:

  • 你的行和列是动态的还是每次都是相同的结构?
  • 生成后一样。
  • 您是如何生成输入字段的名称的?你能展示一些生成的示例标记吗?
  • 您应该使用带有 List&lt;Column&gt; Row 的视图模型,其中 Column 包含 string Value,这样您就可以生成一个强类型的视图并回传给您的模型
  • 如果您在生成表格时使用二维数组作为视图模型,然后发布该视图模型,它将全部为您自动填充

标签: c# asp.net asp.net-mvc twitter-bootstrap


【解决方案1】:

创建代表您想要显示/编辑的视图模型

public class TableVM
{
  public TableVM()
  {
    Rows = new List<RowVM>();
  }
  public TableVM(int rows, int columns) : this()
  {
    for(int i = 0; i < rows; i++)
    {
      Rows.Add(new RowVM(columns));
    }
  }
  public List<string> Headers { get; set; } // for columns headers
  public List<RowVM> Rows { get; set; }
}
public class RowVM
{
  public RowVM()
  {
    Cells = new List<CellVM>();
  }
  public RowVM(int columns) : this()
  {
    for(int i = 0; i < columns; i++)
    {
      Cells.Add(new CellVM());
    }
  }
  public List<CellVM> Cells { get; set; }
}
public class CellVM
{
  public string Value { get; set; }
}

并在GET方法中,初始化一个新的TableVM并返回给视图

TableVM model = new TableVM(5, 5); // 5 x 5 grid
model.Headers = new List<string>{ "col 1", "col 2", "col 3", col 4", "col 5" };
return View(model);

查看

@model TableVM
....
<table>
  <thead>
    <tr>
      @foreach(string header in Model.Headers)
      {
        <th>@header</th>
      }
    </tr>
  </thead>
  <tbody>
    @for(int r = 0; r < Model.Rows.Count; r++)
    {
      <tr>
        for (int c = 0; c < Model.Rows[r].Cells.Count; c++)
        {
          <td>@Html.TextBoxFor(m => m.Rows[r].Cells[c].Value</td>
        }
      </tr>
    }
  </tbody>
</table>

然后更改您的 POST 方法以接受模型

[HttpPost]
public ActionResult Enter(TableVM model)

您现在可以使用索引器访问每一行/列,例如model.Rows[1].Cells[2].Value 将返回第二行中第三列的值

【讨论】:

  • 感谢您的支持,我将尝试您的想法,请告诉我,如果我想在发布操作中迭代第一列,然后是第二列,依此类推。能给我看看代码吗?
  • 很多方法取决于你想做什么 - 例如。如果您想合计第二列中的值int total = 0; foreach(var row in model.Rows) { total += row.Cells[1].Value; }; (but that assumes the Value` 属性是int,而不是string)/我也会很快更新他以展示如何添加构造函数来自动生成行和列跨度>
  • 查看更新以简化使用构造函数生成行和列。
  • 非常感谢@Stephen,它有效我只需要再知道一件事,您显示代码的方式适用于简单的文本框。但是我是否将表单控件类与 TextBoxFor 一起使用,然后它会结束并且不显示水平滚动条并显示简单的文本框。你能告诉我那个解决方案吗?谢谢,我在视图的顶行通过分配自定义宽度完成了此操作。
  • 对不起,我不确定你的意思。您指的是什么滚动条(查看问题中的图片,我可以看到任何滚动条)
【解决方案2】:

最简单的方法是用输入值返回行数和列数:

<div class="container" style='width:@(240 * Model.Columns.Capacity)px;overflow-y:auto'>
<div class="row">
    @using (Html.BeginForm())
    {
        <table style='width:100%' class='table table-condensed'>
            <tbody>
                @for (int k = 0; k < Model.Rows.Capacity; k++)
                {
                    <tr>
                        @for (int i = 0; i < Model.Columns.Capacity; i++)
                        {
                            <td>
                                @Html.TextBox("name" + k + i, null, new { @class = "form-control" })
                            </td>
                        }
                    </tr>
                }
            </tbody>
        </table>

        <input name="rows" type="hidden" value="@Model.Rows.Capacity"/>
        <input name="columns" type="hidden" value="@Model.Columns.Capacity"/>

        <input type="submit" name="name" value="Submit" class="btn btn-primary" />
    }
</div>

一旦你有了这些,你就可以用它们来计算要进行多少次迭代以及要查询的输入名称:

[HttpPost]
public ActionResult Enter(FormCollection form)
{
    var data = new List<List<string>>();
    var rows = int.Parse(form["rows"]);
    var columns = int.Parse(form["columns"]);

    for (var r = 0; r < rows; r++)
    {
        var rowData = new List<string>();

        for (var c = 0; c < columns; c++)
        {
            rowData.Add(form[string.Format("name{0}{1}", r, c)]);
        }

        data.Add(rowData);
    }

    return View(data);
}

【讨论】:

    【解决方案3】:
    to get value horizontally you have to put name attribute in your view like
    <table class="table table-striped table-advance table-hover">
        <thead>
            <tr>
                <th class="col-wg-15">Name</th>
                <th class="col-wg-5 t1_td_per">Allocation Percentage</th>
                <th class="col-wg-5">Allocation</th>
            </tr>
        </thead>
     <tbody>
        @{
        foreach (var item in Model.t1_List)
        {
            <tr>
                <td>
                    @item.Name
                </td>
                <td class="t1_td_per">
                    <input type="text" name="t1_txtper-@item.BId" id="t1_txtper-@item.BId" value="@item.AllocationPercentage" class="t1_AllocationPer" maxlength="20" placeholder="Allocation Percentage" style="text-align:right;" />
                </td>
                <td>
                    <input type="text" name="t1_txtamt-@item.BId" id="t1_txtamt-@item.BId" value="@item.AllocationAmount" class="t1_AllocationAmt decimalval" maxlength="20" placeholder="Allocation" style="text-align:right;" />
                </td>
            </tr>
        }
        }
    </tbody>
    </table>
    
    and then at the time of post or in action method you can get this as 
    
      Dictionary<decimal, decimal> _d_amt = new Dictionary<decimal, decimal>();
      Dictionary<decimal, decimal> _d_per = new Dictionary<decimal, decimal>();
    
                foreach (var key in _frm.AllKeys)
                {
                    if (key.ParseString().Contains("t1_txtamt-"))
                    {
                        decimal _val = _frm[key].ParseString().ParseDecimal();
                        decimal _id = key.Replace("t1_txtamt-", "").ParseString().ParseDecimal();
    
                        _d_amt.Add(_id, _val);
                    }
                    else if (key.ParseString().Contains("t1_txtper-"))
                    {
                        decimal _val = _frm[key].ParseString().ParseDecimal();
                        decimal _id = key.Replace("t1_txtper-", "").ParseString().ParseDecimal();
    
                        _d_per.Add(_id, _val);
                    }
                }
    
                foreach (var item in _model.t1_List)
                {
                    AllocationClass _litem = new AllocationClass();
                    _litem.BId = item.BId;
                    _litem.Name = item.Name;
                    _litem.AllocationPercentage = (from p in _d_per where p.Key == item.BId select p.Value).FirstOrDefault().ParseDecimal();
                    _litem.AllocationAmount = (from p in _d_amt where p.Key == item.BId select p.Value).FirstOrDefault().ParseDecimal();
    
                    _list.Add(_litem);
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-15
      • 2016-06-20
      • 2021-09-18
      相关资源
      最近更新 更多