Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing @Html.DisplayFor() - values from view to controller

I'm having these classes:

public class ProductViewModel
{
    public IEnumerable<Product> Products { get; set; }

}

public class Product
{
    public int ArticeNr { get; set; }
    public string Name { get; set; }

}

And this view

@model ProductsViewModel

@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<table class="table">

    @foreach (var item in Model.Products) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.ArticleNr)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
        </tr>
    }
</table>

<input type="submit" value="submit" name="submitButton" />
}

How can I pass All my values from Model.Products to the Controller? I want all my values in the foreach-loop sent to the controller

My controller is taking ProductsViewModel as parameter. But the values after I post in model.Products is null.

        public ActionResult Index(ProductsViewModel model) //here, model.Products is always null after post
        {
            //LOGIC
        }
like image 598
user3228992 Avatar asked Nov 10 '22 02:11

user3228992


1 Answers

@Html.DisplayFor just displays the value in the view.

If you want the value to be passed back to the controller on post then you also need a @Html.HiddenFor

like image 162
Cuteboy_Max Avatar answered Nov 14 '22 23:11

Cuteboy_Max