public IEnumerable<ModelSpecCreationViewModel> RetrieveSpecification(ModelSpecViewModel item) { var getItemDesc = (from itemObject in db.Items join model in db.Models on itemObject.ItemId equals model.ItemId select itemObject.Description).FirstOrDefault(); if (getItemDesc != null) { var getSpec = from specs in db.Specifications join tags in db.SpecialCategories on specs.SpecialCatId equals tags.SpecialCatId where tags.Description == getItemDesc select specs; var getModelSpec = (from modelSpecs in db.ModelSpecs join specs in db.Specifications on modelSpecs.SpecificationId equals specs.SpecificationId join tags in db.SpecialCategories on specs.SpecialCatId equals tags.SpecialCatId where tags.Description == getItemDesc select modelSpecs); var modelSpecList = new List<ModelSpecCreationViewModel>(); var modelSpec = new ModelSpecCreationViewModel(); if (getSpec != null) { foreach (var spec in getSpec) { modelSpec.ModelSpecId = item.ModelSpecId; modelSpec.ModelId = item.ModelId; modelSpec.SpecificationId = spec.SpecificationId; modelSpec.Description = spec.Description; modelSpec.Value = ""; modelSpecList.Add(modelSpec); } return modelSpecList.ToList(); //foreach (var modSpec in getModelSpec) //{ // var modelSpec = new ModelSpecCreationViewModel() // { // ModelId = modSpec.ModelId, // SpecificationId = spec.SpecificationId, // Description = (from specs in db.Specifications // where specs.SpecificationId == spec.SpecificationId // select specs.Description).FirstOrDefault(), // Value = "" // }; // modelSpecList.Add(modelSpec); //} } return null; } return null; }
public ActionResult ModelSpecCreation(FormCollection collection) { try { string userId = User.Identity.GetUserId(); if (userId == null) { return RedirectToAction("Login", "Account"); } var getSupplierId = (from supplier in db.Suppliers where supplier.Id == userId select supplier.SupplierId).FirstOrDefault(); //Check if the "Supplier" role exists if not it returns a null value var role = db.Roles.SingleOrDefault(m => m.Name == "Supplier"); ModelViewModel model = Session["ModelSpecData"] as ModelViewModel; if (User.IsInRole("Supplier")) { int item = 0; int supplierId = getSupplierId; if (ModelState.IsValid) { //All the specifications that are displayed are stored in the array specIdArray var specIdArray = collection.GetValues("item.SpecificationId"); //All the values of the specifications are stored in the array value valueArray var valueArray = collection.GetValues("item.Value"); //Use a for loop to add a new Model Specificaiton record into the ModelSpecification table for (item = 0; item < valueArray.Count(); item++) { ModelSpecViewModel modelSpec = new ModelSpecViewModel(); //if the supplier does not fill the one of the fields if (String.IsNullOrWhiteSpace(valueArray[item])) { //The value of that field is automatically assigned the string n/a (not available) valueArray[item] = "n/a"; } //Assing the value of the current field to the property of the modelSpec value modelSpec.Value = valueArray[item]; //Assign the specificationId of the specification displayed to the property of modelSpec SpecificationId modelSpec.SpecificationId = Convert.ToInt32(specIdArray[item]); //Assing the modelId of the current model to the property of modelSpecification ModelId modelSpec.ModelId = model.ModelId; modelSpec.SupplierId = supplierId; modelSpec.UserId = userId; //Add a new modelspec record and save changes in the database db.ModelSpecs.Add(modelSpec); db.SaveChanges(); } return RedirectToAction("SupplierRetrieveModels", "Supplier", new { message = ManageMessageId.AddModelSuccess }); } GetSupplierNotification(); return View(); } return RedirectToAction("Login", "Account"); } catch (Exception ex) { return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.ToString()); } }