1

I have a table:

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

After button click I want to collect data like

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

Where in

[{a,b}] a=className of row, b=input text value in this row.

And post this data to action method, what is the best way to do this?

2
  • before asking what's the best way, show us a way you tried... Commented Apr 10, 2012 at 10:31
  • I know, I can iterate table, a collect Data. but how to pass this data to asp.net mvc action Method? Commented Apr 10, 2012 at 10:33

1 Answer 1

3

As i see u have two fields to collect data from each row namely Class and Transfer count. i would make a view model for it like

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

In my view i would write a loop that creates following 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>

supposing that you are posting the form to index method. it would look like

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

don't forget the submit button to post the form. For more information read this article
Following search would also prove good for you

Sign up to request clarification or add additional context in comments.

1 Comment

name attributes are important in mvc model binding

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.