-1

I am developing MVC 4 application in which I need to pass the updated value from the view to controller.

          @foreach (var item in Model)
          {
           <tr>
              <td>@item.ProductId</td>
              <td>@item.Product.ProductName</td>
              <td>@item.Product.UnitPrice</td>
              <td>
                @Html.TextBox("QuantityBox", item.Quantity)
              </td>

             </tr>
            }


           //Update Button

            @using (Html.BeginForm("UpdateCart", "Cart", FormMethod.Post))
            {
                @Html.AntiForgeryToken()
                <input type="submit" id="submit" name="Update" value="Update" />
            }

The values could be entered different for different rows. I need to pass these values from quantity textbox to controller.

Rohit
  • 10,056
  • 7
  • 50
  • 82

4 Answers4

1

Try using a for loop

@using (Html.BeginForm("UpdateCart", "Cart", FormMethod.Post))
{
    @for(int idx = 0;idx < Model.Length;idx++)
    {
     <tr>
        <td>@Model[idx].ProductId</td>
        <td>@Model[idx].Product.ProductName</td>
        <td>@Model[idx].Product.UnitPrice</td>
        <td>
          @Html.TextBoxFor(_ => Model[idx].Quantity)
        </td>
       </tr>
    }

    @Html.AntiForgeryToken()
    <input type="submit" id="submit" name="Update" value="Update" />
}

When you post the above back to the controller, the MVC model binder will see the textboxes for Model[0].Quantity, Model[1].Quantity, etc and try to bind them to the incoming model. Using a foreach loop will result in none of that data being passed back to the controller.

I do not know what @model you are using on your view, but I'm assuming the MVC model binder will be able to deal with this.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
0
Write like this..


@using (Html.BeginForm("UpdateCart", "Cart", FormMethod.Post))
            {
                @Html.AntiForgeryToken()

 @foreach (var item in Model)
          {
           <tr>
              <td>@item.ProductId</td>
              <td>@item.Product.ProductName</td>
              <td>@item.Product.UnitPrice</td>
              <td>`enter code here`
                @Html.TextBox("QuantityBox", item.Quantity)
              </td>

             </tr>
            }
                <input type="submit" id="submit" name="Update" value="Update" />
            }
Naveen Katakam
  • 400
  • 1
  • 8
  • 23
0

I would move the entire Foreach Codeblock

@foreach (var item in Model)
      {
       <tr>
          <td>@item.ProductId</td>
          <td>@item.Product.ProductName</td>
          <td>@item.Product.UnitPrice</td>
          <td>
            @Html.TextBox("QuantityBox", item.Quantity)
          </td>

         </tr>
        }

Into the Using statement : Otherwise your model back in the controller would not include the updated values that the user submmitted in the view.

In other words : the values that you want in your controller has to be in your Using statement where your submit button is located.

Anubis77
  • 102
  • 1
  • 6
-1

Your code should be like below

@using (Html.BeginForm("UpdateCart", "Cart", FormMethod.Post))
                {
     @for(i=0;i< Model.item.count ;i++)
              {
               <tr>
                  <td>@item.ProductId</td>
                  <td>@item.Product.ProductName</td>
                  <td>@item.Product.UnitPrice</td>
                  <td>
                    @Html.TextBoxFor(m=>m.item[i].Quantity)
                  </td>

                 </tr>
                }


               //Update Button


                    @Html.AntiForgeryToken()
                    <input type="submit" id="submit" name="Update" value="Update" />
                }

Then you will get those values in controller through posting Need to use form tag properly let me know if it works If you want to know about url rewriting in MVC-4 please visit http://grandhah.blogspot.in/

Sreerejith S S
  • 258
  • 5
  • 18