How to get table data from asp.net mvc 4 view

My question is how to return table data to controller from view?

I have a class in my model:

public class Company
{
    public string Name { get; set; }
    public int ID { get; set; }
    public string Address { get; set; }
    public string Town { get; set; }
}

      

and I pass the list of companies to my opinion:

    @model IEnumerable<MyTestApp.Web.Models.Company>
    ....
    @using (Html.BeginForm("Edit", "Shop"))
    {
    <table id="example">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Name)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Address)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Town)
                </th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model) {
                <tr>
                    <td>
                        @Html.EditorFor(modelItem => item.Name)
                    </td>
                    <td>
                        @Html.EditorFor(modelItem => item.Address)
                    </td>
                    <td>
                        @Html.EditorFor(modelItem => item.Town)
                    </td>
                </tr>
            }   
        </tbody>
    </table>

    <input type="submit" value="Submit" />
}

      

And everything looks fine, but I can't figure out how to get the changed data in the controller? I have used these approaches:

public ActionResult Edit(IEnumerable<Company> companies)
    {
        // but companies is null
        // and ViewData.Model also is null

        return RedirectToAction("SampleList");
    }

      

I need access to modified objects, what am I doing wrong?

UPDATE: Thanks to webdeveloper , I just needed to use a 'for' loop instead of a foreach loop. Correct version

<tbody>
        @for (int i = 0; i < Model.Count(); i++ ) {
            <tr>                   
                <td>
                    @Html.EditorFor(modelItem => modelItem[i].Name)
                </td>
                <td>
                    @Html.EditorFor(modelItem => modelItem[i].Address)
                </td>
                <td>
                    @Html.EditorFor(modelItem => modelItem[i].Town)
                </td>
            </tr>
        }   
    </tbody>

      

+3


source to share


2 answers


Please see my answer here: Updating multiple items in the same view or for Darin Dimitrov's answer .



You need elements with an attribute index

in name

in the rendered html markup. Also you can see: binding model to list

+2


source


I think you are missing an id Company

in your form to bind the model correctly.

You have to add it like this:



@using (Html.BeginForm("Edit", "Shop"))
{
<table id="example">
    <thead>
        <tr>
            <th>
                @Html.HiddenFor(model => model.ID)
                @Html.DisplayNameFor(model => model.Name)
            </th>
            ...

      

Otherwise, the rest of your code looks fine.

+1


source







All Articles