英文:
Manipulating an entity before saving to DB
问题
I understand your code translation request. Here's the translated 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 + "; ");
// 我尝试了两种不同的方法来连接字符串
ccRecipients = email.Trim() + "; ";
}
// 这是我尝试将字符串操纵到字段的地方
// 据我理解,这是可能的,但我可能错了
newRequest.CCRecipients = ccRecipients;
}
await _changeControlContext.AddAsync(newRequest);
await _changeControlContext.SaveChangesAsync();
// 正在执行的其他代码
return new RedirectToPageResult("RedirectsToAnotherPage");
}
请注意,这只是代码的翻译部分,没有其他内容。如果您有其他问题,请随时提出。
英文:
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
答案1
得分: 1
将现有的代码行
ccRecipients = email.Trim() + "; ";
替换为
ccRecipients = ccRecipients + email.Trim() + "; ";
或者使用更好的方法(由@Masoud Andalibi提到)
ccRecipients += email.Trim() + "; ";
英文:
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() + "; ";
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论