user1075940
user1075940

Reputation: 1125

Get model from partial view to controller

Is it possible to pass model to controller with @Html.Partial?

Using plain <td> in view return list of rows, while this exact same <td> wrapped in partial returns null on form submit

   @using ( Html.BeginForm() )
        {
            <table>
                <tr>
                    <th></th>
                </tr>
                <tr>
                    @for ( int i = 0; i < Model.Rows.Count; i++ )
                    {
                        @*<td class="row">Book @Html.DisplayFor(x => Model.Rows[i].RowNum) @Html.DropDownListFor(x => Model.Rows[i].Selected, Model.Rows[i].Data)  </td>*@
                        @Html.Partial("Row", Model.Rows[i])
                    }
                </tr>
            </table>
            <input type="submit" value="Submit" />
        }

Upvotes: 1

Views: 1057

Answers (2)

scartag
scartag

Reputation: 17680

Html.Partial renders a partial view. If your view is already expecting a model of that type, you can pass it along.

@Html.Partial("ViewName", Model)

To send values to an action of a controller, you can try using Html.Action

@Html.Action("actionName","ControllerName", new {RowNum = Model.Row[i].RowNum})

You can pass values using the anonymous object

Upvotes: 1

ermagana
ermagana

Reputation: 1230

You probably want to use the RenderPartial Extension, it should give you what you need.

Upvotes: 0

Related Questions