Click here to Skip to main content
15,881,665 members
Articles / Web Development / HTML

MVC Techniques with jQuery, JSON, Knockout, and C#

Rate me:
Please Sign up or sign in to vote.
4.94/5 (138 votes)
2 Jan 2012CPOL14 min read 434.3K   22.4K   415  
Developing an Order Entry application with MVC.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NorthwindViewModel;
using NorthwindDataModel;
using NorthwindDataAccessServices;
using NorthwindBusinessServices;

namespace NorthwindApplicationServices
{
    public class ProductApplicationService
    {

        /// <summary>
        /// Get Product Information
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public ProductViewModel GetProductInformation(int productID)
        {
            Products product = new Products();
            ProductViewModel productViewModel = new ProductViewModel();
      
            if (productID == 0)
            {
                List<String> returnMessage = new List<String>();
                returnMessage.Add("An invalid product ID was entered.");
                productViewModel.ReturnMessage = returnMessage;
                productViewModel.ReturnStatus = false;
                return productViewModel;
            }

            ProductDataAccessService productDataAccessService = new ProductDataAccessService();
        
            product = productDataAccessService.GetProductInformation(productID);
            productViewModel.Product = product;
            productViewModel.ReturnStatus = true;

            if (product.ProductID == 0)
            {
                List<String> returnMessage = new List<String>();
                returnMessage.Add(productID.ToString() + " is not a valid product ID");
                productViewModel.ReturnMessage = returnMessage;
                productViewModel.ReturnStatus = false;
            }
          
            return productViewModel;

        }


        /// <summary>
        /// Product Inquiry
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public ProductViewModel ProductInquiry(ProductViewModel viewModel)
        {

            int totalProducts;

            List<String> returnMessage = new List<String>();

            ProductDataAccessService productDataAccessService = new ProductDataAccessService();

            try
            {
                List<Products> products = productDataAccessService.ProductInquiry(viewModel.Product,
                    viewModel.CurrentPageNumber, viewModel.SortExpression, viewModel.SortDirection,
                    viewModel.PageSize, out totalProducts);

                viewModel.TotalProducts = totalProducts;
                viewModel.TotalPages = Utilities.CalculateTotalPages(totalProducts, viewModel.PageSize);
                viewModel.Products = products;

                if (viewModel.TotalProducts > 0 && viewModel.TotalPages < viewModel.CurrentPageNumber)
                {
                    returnMessage.Add("Please resubmit your request.");
                    viewModel.ReturnMessage = returnMessage;
                    viewModel.ReturnStatus = false;
                }
                else
                {
                    returnMessage.Add(totalProducts.ToString() + " products found.");
                    viewModel.ReturnMessage = returnMessage;
                    viewModel.ReturnStatus = true;
                }

            }
            catch (Exception e)
            {
                returnMessage.Add("An application error has occurred. Please call technical support.");
                viewModel.ReturnStatus = false;
            }

            viewModel.ReturnMessage = returnMessage;

            return viewModel;

        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer Joey Software Solutions
United States United States
Mark Caplin has specialized in Information Technology solutions for the past 30 years. Specializing in full life-cycle development projects for both enterprise-wide systems and Internet/Intranet based solutions.

For the past fifteen years, Mark has specialized in the Microsoft .NET framework using C# as his tool of choice. For the past four years Mark has been implementing Single Page Applications using the Angular platform.

When not coding, Mark enjoys playing tennis, listening to U2 music, watching Miami Dolphins football and watching movies in Blu-Ray technology.

In between all this, his wife of over 25 years, feeds him well with some great home cooked meals.

You can contact Mark at mark.caplin@gmail.com

...

Comments and Discussions