如何使用邮政 asp.net 使 CC 字段可选

How to make CC field optional using postal asp.net

我在我的 asp.net mvc 应用程序中使用邮政服务发送电子邮件现在的问题是我必须仅在特定情况下发送抄送电子邮件并且我必须将此字段设置为可选但我不是到目前为止已经实现了这一点。以下是我的代码:

查看文件:

@model OAC.Helpers.EmailHelper

To: @Model.To
Form: @Model.From
CC: @Model.CC
Subject: @Model.Subject


Hello,

@Model.Body

@Model.Link

public class EmailHelper : Email
{
    public string To { get; set; }
    public string CC { get; set; }
    public string Body { get; set; }
    public string Subject { get; set; }
    public string From { get; set; }
    public string Link { get; set; }
    public static void SendEmailNotification(string emailAddress, string body, string link, string subject, string from = "OAC Support", string CC = null)
    {
        // Prepare Postal classes to work outside of ASP.NET request
        var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(@"~/Views/Emails"));
        var engines = new ViewEngineCollection();
        engines.Add(new FileSystemRazorViewEngine(viewsPath));
        var emailService = new EmailService(engines);
        var email = new EmailHelper
        {
            To = emailAddress,
            CC = CC, 
            Body = body,
            Subject = subject,
            From = from,
            Link = link
        };
        emailService.Send(email);
    }
}

现在我想让视图中的 CC 字段成为可选字段,这样当 CC 为 "null" 时,视图会忽略“@Model.CC”字段。内容如下:

@model OAC.Helpers.EmailHelper

To: @Model.To
Form: @Model.From
if(Model.CC != null)
{
 CC: @Model.CC
}
Subject: @Model.Subject


Hello,

@Model.Body

@Model.Link

求助!谢谢。

在道具上使用 [可选] 属性。

[可选] public 字符串 CC { 得到;放; }

提案

您可以使用模型发送数据。

public class EMailModel
{

        public string To { get; set; }
        [Optinal]
        public string CC { get; set; }
        public string Body { get; set; }
        public string Subject { get; set; }
        public string From { get; set; }
        public string Link { get; set; }       
 }

你已经很接近了,只需更改视图即可。在 CC 之前需要“@:”:@Model.Cc

例如

@if (Model.Cc != null)
{
    @:CC: @Model.Cc
}