Nicholas Mabe

Nicholas Mabe

  • 1.6k
  • 72
  • 4.3k

How to capture a value inside a selectbox column, and concatenate?

Jan 11 2021 6:07 PM
I'm trying to build a system that creates a reference number, using an ID created when a new record is saved.. 
The user makes a series of selections using a dropdown lists in a view and then presses Submit. Based on the unique ID and the values Selected in the DropDownLists I want to make a ReferenceNumber consisting of The UniqueID from the Proposals Table and Initials that reside in the DropDownLists source Tables. For example:
 
Proposal.cs
ID
ProposalCategoryID
ProposalReference 
 
ProposalCategory.cs  
ProposalCategoryId
ProposalCategoryName
ProposalCategoryIntials
 
On the View Page the user makes a series of SelectBox choices and presses Submit. This calls an IACTION with end up with an HttpPost Action 
 
I have been able to save the new record, capture the new record ID and the ProposalCategoryId by Saving and capturing the ID into an int called: myId and a var called catId
 
I need to somehow get the ProposalCategoryIntials, I've tried so many methods but so far have failed. I did set up var PropoInitial = _unitOfWork.ProposalCategory.GetAll(); which when I put a breakpoint on I discover all the three fields from the Category database are in there. and all the field names are in there. All I need is the ProposalCategoryIntials based on the CategoryID that was posted. I can see it in the BreakPoint information but I cannot work out how to get it.

 Once I have it I want to add it to the string, I have ID and ProposalCategory in there so far:
 
 proposalVM.Proposal.ProposalReference = "Test New ID: " + "-" + myid + "-" + catid + "-" + PropoInitial;
 
I am using PropoInitial to try to capture the ProposalCategoryIntials, but as PropoInitial has three columns and all data it's causing an error.
 
Once I've built the string I then need to open up the details page send the ID which will then show the generated ProposalReference 
 
My issue is I can't get to  ProposalCategoryIntials and once I have I need to send an ID with the 
 
 return RedirectToAction(nameof(Details)); 
 
Here's the troublesome part of my ProposalController. 
 
ProposalController.cs 
 
[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);
}
_unitOfWork.Save();
{
var catid = proposalVM.Proposal.ProposalCategoryId;
int myid = proposalVM.Proposal.ProposalId;
var PropoInitial = _unitOfWork.ProposalCategory.GetAll();
proposalVM.Proposal.ProposalReference = "Test New ID: " + "-" + myid + "-" + catid + "-" + PropoInitial;
_unitOfWork.Save();
};
return RedirectToAction(nameof(Details));
}
 
Any help is gratefully received.  
 
The IUpsert  process the record is saved, at that point I can capture the unique Id of the new record.
I then want to build a string using that unique ID and values from the SelectListsand

Answers (2)