Ajitender Vijay

Ajitender Vijay

  • 1.4k
  • 263
  • 117.5k

MVC Get view values in controller

Jul 22 2013 7:30 AM
Hi Friends, i am new in MVC, I'm have a product list with textbox and user enter no. of qty in textbox .now i want to get all entered qty in controller on button click



  .csHtml Code

   @model IEnumerable<CSLOrderingMVC.Models.Entity.Product>@{
    Layout = "~/Views/Shared/_Site.cshtml";
    ViewBag.Title = "Product";
}


@using ( Html.BeginForm("SaveQuantity","Products"))
{


<div class="clearfix" style="float: left; position: relative;">
    <table cellpadding="10" cellspacing="2" border="0">
        <tr bgcolor="#fbcaca">
            <td>
                <b>Code</b>
            </td>
            <td>
                <b>Product Name</b>
            </td>
            <td>
                <b>Unit Price</b>
            </td>
            <td>
                <b>Qty</b>
            </td>
        </tr>
        @foreach (var item in Model)
        {
       
            <tr bgcolor="#cccccc">
                <td>
                    @item.ProductCode
                </td>
                <td>
                    @item.ProductName
                </td>
                <td>
                    @item.Price
                </td>
                <td>
                   
                     @Html.TextBox("txtProductQty")
                </td>
            </tr>}
    </table>
</div>




<input type="submit"  value="Proceed to basket"  />
}


Controller Code


        [HttpPost]
        public ActionResult SaveQuantity(FormCollection formCollection)
        {


            foreach (var key in formCollection.Keys)
            {
                var value = formCollection[key.ToString()];
                // etc.
            }




            return View();
        }

Answers (3)