从asp.net mvc中的表中发布值

时间:2012-04-10 10:30:24

标签: asp.net-mvc asp.net-mvc-2

我有一张桌子:

    <table id="selectedInv">
        <thead>
            <tr class="alternate">
                <th>
                    Barcode
                </th>
                <th>
                    OverAll Count
                </th>
                <th>
                    Transfer Count
                </th>
            </tr>
        </thead>
        <tbody>
           <tr class="1">
                <td> 2323 </td><td> 9 </td><td><input type="text" value="3"></td></tr>
           <tr class="2">
                <td> 2329 </td><td> 5 </td><td><input type="text" value="2"></td></tr>
           <tr class="3">
                <td> 2329 </td><td> 3 </td><td><input type="text" value="1"></td></tr>
        </tbody>
    </table>

点击按钮后,我想收集

等数据
  

[{1,3},{2,2},{3,1}]

中的位置
  

[{a,b}] a =行的className,b =此行中的输入文本值。

并将此数据发布到操作方法,这样做的最佳方法是什么?

1 个答案:

答案 0 :(得分:3)

正如我所知,你有两个字段来收集每一行的数据,即Class和Transfer count。我会为它制作一个视图模型,如

public class ViewModel
{
    public int CodeClass{get;set;} //class is reserved word
    public int TransferCount{get;set;}
}

在我看来,我会写一个创建以下html的循环

<table id="selectedInv">
        <thead>
            <tr class="alternate">
                <th>
                    Barkod
                </th>
                <th>
                    OverAll Count
                </th>
                <th>
                    Transfer Count
                </th>
            </tr>
        </thead>
        <tbody>
           <tr class="1">
                <td> 2323 </td><td> 9 </td><td><input name="data[0].TransferCount" type="text" value="3"><input type="hidden" value = "1" name = "data[0].CodeClass"/></td></tr>
           <tr class="2">
                <td> 2329 </td><td> 5 </td><td><input type="text" value="2" name="data[1].TransferCount"><input type="hidden" value = "1" name = "data[1].CodeClass"/></td></tr>
           <tr class="3">
                <td> 2329 </td><td> 3 </td><td><input type="text" value="1" name="data[2].TransferCount"><input type="hidden" value = "1" name = "data[2].CodeClass"/></td></tr>
        </tbody>
    </table>

假设您要将表单发布到索引方法。它看起来像

public ActionResult index(IEnumerable<ViewModel> data)
{
 //do something with data
}

不要忘记发布表单的提交按钮。有关详细信息,请阅读this article
关注search对您也有好处