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


Solution 1:[1]

@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

Solution 2:[2]

Here I will describe how to post a collection in ASP.Net MVC 3 using a sample application. The basic idea behind posting (passing HTML field values from view to controller) is by matching the name field of the rendered HTML control with receiving parameters in the Controller's action method.
ASP.Net MVC - How to Post a Collection

Solution 3:[3]

As I see you are not modifying your values in the foreach loop - so you are posting back the same values your view received for display. In that case keep your model class as is but change your index view and home controller as follows

view:

   @model Models.ProductViewModel



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

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

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

controller:

    [HttpGet]
    public ActionResult Index()
    {           
        return View(new ProductViewModel()); // this model contains products you want to display
    }

    [HttpPost]
    public ActionResult Index(ProductViewModel model)
    {
        // do smth with your model here
        return View();
    } 

I suspect your problems were the result of your actions not having the attributes [HttpGet] and [HttpPost]

Solution 4:[4]

i have one example. This is View send back form values on button submit and this values binded from another contoller action.

 @using (Html.BeginForm("Add", "Product", FormMethod.Post))
{
    @Html.ActionLink("CheckOut", "Checkout", new { @class = "btn btn-success" })
    foreach (var item in Model)
    {
        <div class="row">
            <div class="col-lg-3">
                <img id="img" src="@Html.DisplayFor(modelItem => item.img)" alt="img" />
            </div>
            <div class="col-lg-3">
                <input type="text" name="name" value="@item.name" border="0" />
                <br />
                <input id="text1" name="quantity" type="number" placeholder="Quantity" class="form-control text-muted" style="width:100px" />
                <br />
                <span>Price(per Kg):&nbsp;&nbsp;</span> &#8377;
                <input type="text" name="price" value="@item.price" border="0" />
                <br />
                <input id="Button1" type="submit" value="ADD" />
            </div>
            <div class="col-lg-6">
            </div>
        </div>
        <hr />
        <br />
    }
}

and this is action method in controller that recieve values

 public ActionResult Add(string name,int quantity,int price)
    {
        Session["name"] = name;
        Session["quantity"] = quantity;
        Session["price"] = price;
        Session["Total"] = quantity * price;
        return RedirectToAction("UserView");
    }

I think this will help.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cuteboy_Max
Solution 2 Hamix
Solution 3
Solution 4