You must use viewmodels to achieve this. You should not expose your business models directly to scans. Instead, use viewmodels to provide a different level of abstraction.
So, do not change your models at all. Define a presentation model for your presentation. I will not include all fields in the viewmodel.
public class OrderCreate { public string OrderDate { get; set; } public string OrderNo { get; set; } public string Shipping { get; set; } .... .... public IList<ProductDetail> ProductDetails { get; set; } } public class ProductDetail { public string Product { get; set; } public string UnitPrice { get; set; } public string Quantity { get; set; } .... .... }
Now in your GET action, return this view model to your view instead of your business model.
// // GET: /Order/Create public ActionResult Index() { /* New viewmodel for your view */ var viewModel = new OrderCreate(); viewModel.ProductDetails = new List<ProductDetail>(); /* Assuming the number of products is static */ for(int i = 0; i < NUMBER_OF_PRODUCTS; i++) { viewModel.ProductDetails.Add( new ProductDetail() ); } return View(viewModel); }
With this view model, you can now access the values ββpopulated by your view. When you publish your opinion for publication, create your business model using the data from the view model.
// // POST: /Order/Create [HttpPost] public ActionResult Index(OrderCreate viewModel) { if(ModelState.IsValid)) { var model = new Order(); //TODO: Populate model through viewmodel, loop viewModel.ProductDetails return RedirectToAction("Index"); } // Model is not valid return View(viewMode); }
Some tips if you are bored of comparing your view models with real models, try AutoMapper
Hope this helps.
source share