5

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
        }
8
  • 2
    DisplayFor() does not create inputs for postback. You need to use methods that generate <input> or <select> elements such as EditorFor(), TextBoxFor(), HiddenFor() .... Commented Aug 29, 2014 at 7:07
  • @StephenMuecke Thanks for the answer. I've tried to set a HiddenFor() on each property in my foreach-loop but still is getting null on the postBack to controller. WhatI'm I missing out? Commented Aug 29, 2014 at 7:10
  • Your not constructing the loop correctly. Your need for(int i = 0; i < Model.Projects.Count; i++) { @Html.HiddenFor(m => m.Projects[i].Name) ...But why on earth are you posting back a whole lot of hidden inputs? Commented Aug 29, 2014 at 7:12
  • 1
    From a performance point of view, getting the collection again from the database is usually always going to be better, although I'm unsure why you would need to get the collection again when saving the model Commented Aug 29, 2014 at 7:20
  • 1
    For performance, I would suggest you use AJAX (jquery $.get() function to pass a value(s) to an action method, and that method calls the database to filter the results based on the value(s) and return a partial view that can be updated in the view - i.e. you don't need to reconstruct the whole page. Commented Aug 29, 2014 at 7:29

4 Answers 4

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

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

Comments

0

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

Comments

0

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]

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.