0

I have an issue understanding something here. I'm trying to manipulate a field from an entity that is being created, but it is not working as expected.

In short I'm trying to change a field that has emails by splitting them and then adding a ; separator to distinguish the different emails before saving to the database.

This is my code:

public async Task<IActionResult> OnPost(Models.Requests.Request newRequest)
{
    newRequest.RequestStatusID = (int)RequestStatusEnum.Requested;
    newRequest.CreatedBy = User.Identity.Name;
    newRequest.CreatedDate = DateTime.Now;
    newRequest.LastRequestDate = DateTime.Now;

    if (!String.IsNullOrEmpty(newRequest.CCRecipients))
    {
        string[] emailSeparators;
        string ccRecipients = "";
        emailSeparators = newRequest.CCRecipients.Split(new Char[] { ' ', ',', ';' });

        foreach (var email in emailSeparators)
        {
            //ccRecipients.Concat(email + "; ");
            
            // I have tried two different ways to concatenate the string
            ccRecipients = email.Trim() + "; ";
        }

        // This is where I'm trying to manipulate the string into the field
        // To my understanding this is possible but I could be wrong
        newRequest.CCRecipients = ccRecipients;
    }

    await _changeControlContext.AddAsync(newRequest);
    await _changeControlContext.SaveChangesAsync();

    // Other code that is being done

    return new RedirectToPageResult("RedirectsToAnotherPage");
}

There's probably a better way to handle this but for now I want to understand why this is not working or what am I over looking.

In theory it should work but it is not. What could be the issue here?

EDIT: when I mean not working i mean that the values of CCRecipients field of said request are not being save in the database.

For example: RandomEmail@test.com; RandomEmail2@test.com. But in the database, nothing appears is stored. It's a white space instead of null. Sorry for the lack of clarity

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
DonDavid12
  • 13
  • 4
  • 1
    "Not working" is not a good description of problem. Not working how??? Error out? Not changing? Changing to some random value??? – Eric Aug 04 '23 at 22:52
  • Ah yes sorry for the lack of clarity. I have edited the post. Thank you for pointing that out – DonDavid12 Aug 04 '23 at 22:57
  • Have you put breakpoint inside your `foreach` to check the value of email variable? – Eric Aug 04 '23 at 23:00
  • How it is currently set it is a complete nightmare. No local development to test any change I have to deploy the project. This is a project from work. Don't know why this is setup the way it is but I'm fighting for my life (Like Ray J would say) to change this. In short it is not possible to add a breakpoint – DonDavid12 Aug 04 '23 at 23:03
  • I am not sure how I can help if you can't even help yourself. – Eric Aug 04 '23 at 23:08

1 Answers1

1

Replace the existing line of code

ccRecipients = email.Trim() + "; ";

with

ccRecipients = ccRecipients + email.Trim() + "; ";

OR with a better approach (as mentioned by @Masoud Andalibi)

ccRecipients += email.Trim() + "; ";

Abhishek Vyas
  • 599
  • 1
  • 9
  • 24
  • 1
    lol just noticed his mistake. although `ccRecipients += email.Trim() + "; ";` is a better approach. – Masoud Andalibi Aug 05 '23 at 06:43
  • 1
    OMG. I can't believe I forgot the + sign. That was the missing piece thank you very much. I knew something ws up but couldn't see it lol – DonDavid12 Aug 08 '23 at 16:54