All except the ID column, the other columns and hence their values in a SelectList are not currently present in my ProposalRepository.cs. Currently only the ID is being sent. As the select list is not present in ProposalRepository.cs so I cannot get access to the columns and values I require.
My question is how do I make the values in the other columns of the SelectList available?
In this update method I am unable to access the additional fields of the select boxes, I am able to access the ID fields of the Select Boxes.
I guess I need to create an instance of the SELECTLIST that has the fields that I require in them, not just the ID field in them, but I can’t for the life of me get this to work.
Currently I have this:
var objFromDb = _db.Proposals.FirstOrDefault(s => s.ProposalId == proposal.ProposalId);
if (objFromDb != null)
{
objFromDb.AccountId = proposal.AccountId;
objFromDb.ArmouringId = proposal.ArmouringId;
objFromDb.BaseUnitId = proposal.BaseUnitId;
objFromDb.ProposalDate = proposal.ProposalDate;
objFromDb.ProposalReference = "MEVA" + "-" + proposal.ProposalId + "-" + proposal.ProposalCategoryId + "-" + proposal.BaseUnitId;
objFromDb.ProposalCategoryId = proposal.ProposalCategoryId;
_db.SaveChanges();
}
I need something like this:
var objFromDb = _db.Proposals.FirstOrDefault(s => s.ProposalId == proposal.ProposalId);
if (objFromDb != null)
{
objFromDb.AccountId = proposal.AccountId;
objFromDb.ArmouringId = proposal.ArmouringId;
objFromDb.BaseUnitId = proposal.BaseUnitId;
objFromDb.ProposalDate = proposal.ProposalDate;
objFromDb.ProposalReference = "MEVA" + "-" + proposal.ProposalId + "-" + proposal.ProposalCategoryInitials + "-" + proposal.BaseUnitInitials;
objFromDb.ProposalCategoryId = proposal.ProposalCategoryId;
_db.SaveChanges();
}
The column ProposalReference is made up of a concatenation of numbers and initials, it is generated through the selection made eg: ProposalID + PropoaslCatagegoryIntitials + BaseUnitIntials
The full ProposalRepository.cs file is as follows:
ProposalRepository.c
using MEVASALES.DataAccess.Data;
using MEVASALES.DataAccess.Repository;
using MEVASALES.DataAccess.Repository.IRepository;
using MEVASALES.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MEVASALES.DataAccess.Repository
{
public class ProposalRepository : Repository<Proposal>, IProposalRepository
{
private readonly ApplicationDbContext _db;
public ProposalRepository(ApplicationDbContext db) : base(db)
{
_db = db;
}
public void Update(Proposal proposal)
{
var objFromDb = _db.Proposals.FirstOrDefault(s => s.ProposalId == proposal.ProposalId);
if (objFromDb != null)
{
objFromDb.AccountId = proposal.AccountId;
objFromDb.ArmouringId = proposal.ArmouringId;
objFromDb.BaseUnitId = proposal.BaseUnitId;
objFromDb.ProposalDate = proposal.ProposalDate;
objFromDb.ProposalReference = "MEVA" + "-" + proposal.ProposalId + "-" + proposal.ProposalCategoryId + "-" + proposal.BaseUnitId;
objFromDb.ProposalCategoryId = proposal.ProposalCategoryId;
_db.SaveChanges();
}
}
}
}
The intials properties of the SelectList are not currently present, so I cannot access them. My question is how do I make the present in the ProposalRepository.cs file?
The full IProposalRepository.cs file is as follows:
using MEVASALES.Models;
using System;
using System.Collections.Generic;
using System.Text;
namespace MEVASALES.DataAccess.Repository.IRepository
{
public interface IProposalRepository : IRepository<Proposal>
{
void Update(Proposal proposal);
}
}
The full VWProposal.cs file is as follows:
VWProposal.cs
using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using System.Text;
namespace MEVASALES.Models.ViewModels
{
public class ProposalVM
{
public Proposal Proposal { get; set; }
public IEnumerable<SelectListItem> AccountList { get; set; }
public IEnumerable<SelectListItem> ArmouringList { get; set; }
public IEnumerable<SelectListItem> BaseUnitList { get; set; }
public IEnumerable<SelectListItem> ProposalCategoryList { get; set; }
}
}
In the ProposalController.cs I am achieving the required function when the record is created, but am struggling with the Update method uses in ProposalRepository.cs
The ProposalController.cs file is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MEVASALES.DataAccess.Repository.IRepository;
using MEVASALES.Models;
using MEVASALES.Models.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
namespace MEVASALES.Areas.Admin.Controllers
{
[Area("Admin")]
public class ProposalController : Controller
{
private readonly IUnitOfWork _unitOfWork;
public ProposalController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public IActionResult Details(int id)
{
var proposalFromDb = _unitOfWork.Proposal.
GetFirstOrDefault(u => u.ProposalId == id, includeProperties: "Account,Armouring,ProposalCategory,BaseUnit");
return View(proposalFromDb);
}
public IActionResult Index()
{
return View();
}
public IActionResult Upsert(int? id)
{
ProposalVM proposalVM = new ProposalVM()
{
Proposal = new Proposal(),
AccountList = _unitOfWork.Account.GetAll().OrderBy(s => s.CompanyName).Select(i => new SelectListItem
{
Text = i.CompanyName,
Value = i.AccountId.ToString()
}),
ArmouringList = _unitOfWork.Armouring.GetAll().OrderBy(s => s.ArmouringName).Select(i => new SelectListItem
{
Text = i.ArmouringName,
Value = i.ArmouringId.ToString()
}),
BaseUnitList = _unitOfWork.BaseUnit.GetAll().OrderBy(s => s.BaseUnitName).Select(i => new SelectListItem
{
Text = i.BaseUnitName,
Value = i.BaseUnitId.ToString()
}),
ProposalCategoryList = _unitOfWork.ProposalCategory.GetAll().OrderBy(s => s.ProposalCategoryName).Select(i => new SelectListItem
{
Text = i.ProposalCategoryName,
Value = i.ProposalCategoryId.ToString()
})
};
if (id == null)
{
//this is for create
return View(proposalVM);
}
//this is for edit
proposalVM.Proposal = _unitOfWork.Proposal.Get(id.GetValueOrDefault());
if (proposalVM.Proposal == null)
{
return NotFound();
}
return View(proposalVM);
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Upsert(ProposalVM proposalVM)
{
if (ModelState.IsValid)
{
if (proposalVM.Proposal.ProposalId == 0)
{
_unitOfWork.Proposal.Add(proposalVM.Proposal);
}
else
{
_unitOfWork.Proposal.Update(proposalVM.Proposal);
}
//int myid = proposalVM.Proposal.ProposalId;
//var NewId = myid;
//proposalVM.Proposal.ProposalReference = "Test" + NewId;
_unitOfWork.Save();
{
var catid = proposalVM.Proposal.ProposalCategoryId;
int myid = proposalVM.Proposal.ProposalId;
proposalVM.ProposalCategoryList = _unitOfWork.ProposalCategory.GetAll().OrderBy(s => s.ProposalCategoryName).Where(ProposalCategory => ProposalCategory.ProposalCategoryId == catid).Select(i => new SelectListItem
{
Text = i.ProposalCategoryInitials,
Value = i.ProposalCategoryId.ToString(
});
//var CatInitials = proposalVM.Proposal.ProposalCategory.ProposalCategoryInitials;
//var NewId = myid;
//ViewBag.NewId = NewId;
var PropoInitial = _unitOfWork.ProposalCategory.GetAll().Where(x => x.ProposalCategoryId == catid)
.Select(x => x.ProposalCategoryInitials).SingleOrDefault();
//var PropoInitial = _unitOfWork.ProposalCategory.GetAll();
//proposalVM.Proposal.ProposalReference = "Test New ID: " + "-" + myid + "-" + catid + "-" + PropoInitial;
proposalVM.Proposal.ProposalReference = "MEVA" + "-" + myid + "-" + catid + "-" + PropoInitial;
_unitOfWork.Save();
};
int Propid = proposalVM.Proposal.ProposalId;
return RedirectToAction(nameof(Details), new { id = Propid.ToString() });
}
else
{
proposalVM.AccountList = _unitOfWork.Account.GetAll().OrderBy(s => s.CompanyName).Select(i => new SelectListItem
{
Text = i.CompanyName,
Value = i.AccountId.ToString()
});
proposalVM.ArmouringList = _unitOfWork.Armouring.GetAll().OrderBy(s => s.ArmouringName).Select(i => new SelectListItem
{
Text = i.ArmouringName,
Value = i.ArmouringId.ToString()
});
proposalVM.BaseUnitList = _unitOfWork.BaseUnit.GetAll().OrderBy(s => s.BaseUnitName).Select(i => new SelectListItem
{
Text = i.BaseUnitName,
Value = i.BaseUnitId.ToString()
});
proposalVM.ProposalCategoryList = _unitOfWork.ProposalCategory.GetAll().OrderBy(s => s.ProposalCategoryName).Select(i => new SelectListItem
{
Text = i.ProposalCategoryName,
Value = i.ProposalCategoryId.ToString()
});
if (proposalVM.Proposal.ProposalId != 0)
{
proposalVM.Proposal = _unitOfWork.Proposal.Get(proposalVM.Proposal.ProposalId);
}
}
return View(proposalVM);
}
#region API CALLS
[HttpGet]
public IActionResult GetAll()
{
var allObj = _unitOfWork.Proposal.GetAll(includeProperties: "ProposalCategory,Account,BaseUnit");
return Json(new { data = allObj });
}
[HttpGet]
[HttpDelete]
public IActionResult Delete(int id)
{
var objFromDb = _unitOfWork.Proposal.Get(id);
if (objFromDb == null)
{
return Json(new { success = false, message = "Error while deleting" });
}
_unitOfWork.Proposal.Remove(objFromDb);
_unitOfWork.Save();
return Json(new { success = true, message = "Delete Successful" });
}
#endregion
}
}