Wednesday, 11 January 2017

Call MVC controller action methord in web api


using Nop.Admin.Validators.Equipment;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Equipment;
using Nop.Core.Domain.Models;
using Nop.Web.Framework;
using Nop.Web.Framework.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FluentValidation.Attributes;
namespace Nop.Admin.Models.Equipment
{
    [Validator(typeof(EquipmentValidator))]
    public partial class EquipmentModel : BaseNopEntityModel
    {
        public EquipmentModel()
        {
            AvailableManufacturers = new List<SelectListItem>();
            AvailableModel = new List<SelectListItem>();
            AvailableModelType = new List<SelectListItem>();
            AvailableStatus = new List<SelectListItem>();
            AvailableFilterMaintenance = new List<SelectListItem>();
            EquipmentFilterMaintenanceDtls = new List<EquipmentFilterMaintenance>();
            EquipmentPictureModels = new List<EquipmentPictureModel>();
            AvailableVendors = new List<SelectListItem>();

            AddPictureModel = new EquipmentPictureModel();
            AvailableEquipments = new List<SelectListItem>();
            AvailableEquipmentRentalFrequencies =new List<SelectListItem>();

        }

        [KeyDatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public override int Id { getset; }

        [NopResourceDisplayName("Admin.Equipment.Fields.EquipmentName")]
        public string Name { getset; }

        [NopResourceDisplayName("Admin.Equipment.Fields.EquipmentID")]
        public string AssetID { getset; }
        [NopResourceDisplayName("Admin.Equipment.Fields.SerialNo")]
        public string SerialNo { getset; }
        [NopResourceDisplayName("Admin.Equipment.Fields.ManufacturerID")]
        public int ManufacturerID { getset; }
        public IList<SelectListItem> AvailableManufacturers { getset; }


        [NopResourceDisplayName("Admin.Equipment.Fields.ModelID")]
        public int ModelID { getset; }
        public IList<SelectListItem> AvailableModel { getset; }

        [NopResourceDisplayName("Admin.Equipment.Fields.ModelTypeID")]
        public int ModelTypeID { getset; }
        public IList<SelectListItem> AvailableModelType { getset; }

        [NopResourceDisplayName("Admin.Equipment.Fields.Description")]
        public string Description { getset; }

        [NopResourceDisplayName("Admin.Equipment.Fields.FilterMaintenanceID")]
        [NotMapped]
        public int FilterMaintenanceID { getset; }
        [NotMapped]
        public string PictureThumbnailUrl { getset; }
        public IList<SelectListItem> AvailableFilterMaintenance { getset; }
        public List<EquipmentFilterMaintenance> EquipmentFilterMaintenanceDtls { getset; }

        [NopResourceDisplayName("Admin.Equipment.Fields.SellPrice")]
        public decimal CostPrice { getset; }

        [NopResourceDisplayName("Admin.Equipment.Fields.WarrantyPeriod")]
        public int WarrantyPeriod { getset; }

        public int WarrantyUnit { getset; }
        [NopResourceDisplayName("Admin.Equipment.Fields.StatusID")]
        public int StatusID { getset; }
        public IList<SelectListItem> AvailableStatus { getset; }

        [NopResourceDisplayName("Admin.Equipment.Fields.PurchasedDate")]
        public DateTime? PurchasedDate { getset; }
        [NopResourceDisplayName("Admin.Equipment.Fields.RetiredDate")]
        public DateTime? RetiredDate { getset; }
        [NopResourceDisplayName("Admin.Equipment.Fields.Color")]
        public string Color { getset; }
        public int? CreatedBy { getset; }
        public DateTime? CreatedOnUtc { getset; }
        public int? UpdatedBy { getset; }
        public DateTime? UpdatedOnUtc { getset; }
        public bool IsDeleted { getset; }
        public Manufacturer Manufacturer { getset; }
        public Nop.Admin.Models.EquipmentModel.ModelModel Model { getset; }
        public ModelType ModelType { getset; }
        public Status Status { getset; }

        [NopResourceDisplayName("Admin.Equipment.Fields.isAutomatic")]
        public bool isAutomatic { getset; }
        [NopResourceDisplayName("Admin.Equipment.Fields.isPourOver")]
        public bool isPourOver { getset; }
        [NopResourceDisplayName("Admin.Equipment.Fields.isVendor")]
        public bool isVendor { getset; }
        [NopResourceDisplayName("Admin.Equipment.Fields.vendorId")]
        public int vendorId { getset; }
        public bool? IsEquipmentSold { getset; }
        public IList<SelectListItem> AvailableVendors { getset; }


        public IList<EquipmentPictureModel> EquipmentPictureModels { getset; }
        public EquipmentPictureModel AddPictureModel { getset; }

        public int EquipmentId { getset; }
        public IList<SelectListItem> AvailableEquipments { getset; }

        public EquipmentShortSummaryModel EquipmentShortSummary { getset; }

        //[Validator(typeof(EquipmentValidator))]
        public partial class EquipmentPictureModel : BaseNopEntityModel
        {
            public int EquipmentId { getset; }

            [UIHint("Picture")]
            [NopResourceDisplayName("Admin.Catalog.Equipment.Pictures.Fields.PicturePreview")]
            public int PictureId { getset; }

            [NopResourceDisplayName("Admin.Catalog.Equipment.Pictures.Fields.PictureUrl")]
            public string PictureUrl { getset; }

            [Required(ErrorMessage = "Display order is required.")]
            [NopResourceDisplayName("Admin.Catalog.Equipment.Pictures.Fields.DisplayOrder")]
            public int DisplayOrder { getset; }


            [NopResourceDisplayName("Admin.Catalog.Equipment.Pictures.Fields.IsDefault")]
            public Boolean? IsDefault { getset; }
            public Boolean IsModelPic { getset; } // if the picture came from model the equipment belongs to then this column value will be true. So that the image cannot be deleted
            [Required(ErrorMessage = "Title is required.")]
            [NopResourceDisplayName("Admin.Catalog.Equipment.Pictures.Fields.Title")]
            public string Title { getset; }

            [Required(ErrorMessage = "Alternate Text is required.")]
            [NopResourceDisplayName("Admin.Catalog.Equipment.Pictures.Fields.AlternateText")]
            public string AlternateText { getset; }
        }

        [NopResourceDisplayName("Admin.Equipment.Fields.StandardRentalCharge")]
        public decimal StandardRentalCharge { getset; }
        [NopResourceDisplayName("Admin.Equipment.Fields.EquipmentRentalFrequencyId")]
        public int EquipmentRentalFrequencyID { getset; }
        public IList<SelectListItem> AvailableEquipmentRentalFrequencies { getset; }
        public EquipmentRentalFrequency EquipmentRentalFrequency { getset; }
        [NopResourceDisplayName("Admin.Equipment.Fields.IsTaxable")]
        public bool IsTaxable { getset; }
    }

}
MVC CONROLLER


 public partial class HomeController : Controller
//GET
{
public JsonResult GetEquipmentWebAPI(string assetNo)
        {
            var result = GetEquipment(assetNo);
            var serializer = new JavaScriptSerializer();
            if (result.Data != null)
            {
                //return Json(result, JsonRequestBehavior.AllowGet);
                return Json(serializer.Serialize(result), JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { Id = 0, Value = "Not Available" }, JsonRequestBehavior.AllowGet);
            }
        }
//Post
 public JsonResult CreateEquipmentWebAPI(EquipmentModel model)
        {
            var serializer = new JavaScriptSerializer();
            model.CreatedOnUtc = DateTime.Now;
            model.CreatedBy = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.Id;
            model.UpdatedOnUtc = DateTime.Now;
            EquipmentFilterMaintenance obj = new EquipmentFilterMaintenance();
            obj.FilterMaintenanceID = model.FilterMaintenanceID;

            model.EquipmentFilterMaintenanceDtls.Add(obj);
            var entity = model.ToEntity();
            int id = _equipmentService.InsertEquipment(entity);
            TempData["EquipmentAdded"] = id.ToString() + "," + model.AssetID + "," + _modelService.GetModelById(model.ModelID).Name + "," + model.SerialNo;
            TempData["IsFromBarcodeScanning"] = true;
            TempData["IsNewEquipment"] = true;
            return Json(serializer.Serialize(model.AssetID + ", is Created"), JsonRequestBehavior.AllowGet);
        }
}


Web api controller

  public class HomeController : ApiController
{

 public JsonResult CheckForExistingEquipment(string assetNo)
        {
            var barcodeController = DependencyResolver.Current.GetService<Nop.Admin.Controllers.BarcodeController>();
            var result = barcodeController.GetEquipmentWebAPI(assetNo);
            var serializer = new JavaScriptSerializer();
            return result;
     }

 public JsonResult CreateEquipmentWebAPI(EquipmentModel model)
        {
            var barcodeController = DependencyResolver.Current.GetService<Nop.Admin.Controllers.BarcodeController>();
            var result = barcodeController.CreateEquipmentWebAPI(model);
            return result;
        }
}







No comments:

Post a Comment