Click here to Skip to main content
15,887,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
say, I have a class library name CustomObject. This library holds objects. one of them is Product

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ObjectLayer
{
    [Serializable]
    public class Products
    {
        private int _Id;
        private string _ProductName;
        private string _ProductMakers;
        private int _Price;
        private OrderPlacementModes _ActionTaken;
        public Products()
        {
            Id = 0;
            ProductName = string.Empty;
            ProductMakers = string.Empty;
            Price = 0;
            ActionTaken = OrderPlacementModes.NoAction;

        }

        public int Id
        {
            get
            {
                return _Id;
            }
            set
            {
                _Id = value;
            }
        }
        public string ProductName
        {
            get
            {
                return _ProductName;
            }
            set
            {
                _ProductName = value;
            }
        }
        public string ProductMakers
        {
            get
            {
                return _ProductMakers;
            }
            set
            {
                _ProductMakers = value;
            }
        }
        public int Price
        {
            get
            {
                return _Price;
            }
            set
            {
                _Price = value;
            }
        }
        public OrderPlacementModes ActionTaken
        {
            get { return _ActionTaken; }
            set { _ActionTaken = value; }
        }
    }
}


solution has another project which holds BAL (Business Access Layer) and a DAL (Data Access Layer)

I did in Controller is
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ObjectLayer;
using OrderPlacementBLL;

namespace MvcApplication1.Controllers
{
    public class ProductssController : Controller
    {
        //
        // GET: /Products/

        public ActionResult Index()
        {
            return View(new ProductsBLL().GetAllProducts());
        }

        [HttpPost]
        public ActionResult Index(FormCollection ProductList)
        {

            return View();
        
        }

    }
}


and in view
C#
@model List<ObjectLayer.Products>
<h2>
    Index</h2>
@using (Html.BeginForm("Index", "Productss", FormMethod.Post, null))
{
    foreach (var item in Model)
    {
    <legend>
        <fieldset>
            Id:             @Html.TextBoxFor(txt => item.Id)<br />
            Prict:          @Html.TextBoxFor(txt => item.Price)<br />
            Makers:         @Html.TextBoxFor(txt => item.ProductMakers)<br />
            Product Name:   @Html.TextBoxFor(txt => item.ProductName)<br />
        </fieldset>
    </legend>
    }
    
    <input type="submit" value="Submit" />
    
    
}


now I want, when I pressed submit button, all the data (List<products>) should be submitted to the controller with changes. please help
Posted
Updated 19-Dec-12 19:53pm
v2

1 solution

You should use AJAX for responses in MVC, it's just nicer. But, you can create a controller that handles the same method with HTTPPost, taking your viewmodel object ( which should be a class you define ). Then if you spell 'products' correctly in your BeginForm call, you should find that a submit will call your method that takes the viewmodel, with the values filled in.
 
Share this answer
 
Comments
Faisalabadians 20-Dec-12 2:20am    
[httppost] method got null when I pressed submit button
Christian Graus 20-Dec-12 4:14am    
You mean a null value was passed in ? That's going to be an issue with the setup in terms of having a strongly typed viewmodel class which your view knows it is using as a model
Faisalabadians 20-Dec-12 4:51am    
I have used models but this time, dont know why I am getting it null in controller. and this is not a model, this is object layer (products) which I passed to view and now trying to get it back in controller but ..... !
Christian Graus 20-Dec-12 5:12am    
You need to post code, but read what I said. You need a viewmodel to pass in to the view, so the view can pass it back to you because it's strongly typed

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900