[英]How to make CC field optional using postal asp.net
I am using postal to send email using postal service in my asp.net mvc application now the issue is I have to send CC email in specific cases only and I have to make this field optional but I am not been to achieve this so far. 我正在使用邮政在我的asp.net mvc应用程序中使用邮政服务发送电子邮件现在问题是我必须在特定情况下发送CC电子邮件,我必须使这个字段可选但我到目前为止还没有实现这一点。 Following is my code for this:
以下是我的代码:
View file: 查看文件:
@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);
}
}
Now I want to make the CC field in view optional so that when CC is "null" the view ignores the "@Model.CC" field. 现在我想使视图中的CC字段可选,这样当CC为“null”时,视图忽略“@ Model.CC”字段。 Something as following:
如下:
@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
Kindly Help! 请帮忙! Thank you.
谢谢。
You were very close, just change the View. 你非常接近,只需更改视图。 Need "@:" before the CC: @Model.Cc
在CC之前需要“@:”:@ Model.Cc
eg 例如
@if (Model.Cc != null)
{
@:CC: @Model.Cc
}
Use [Optinal] attributes on your prop. 在道具上使用[Optinal]属性。
[Optinal] public string CC { get; [Optinal] public string CC {get; set;
组; }
}
proposal 提案
You can use the model to send the data. 您可以使用该模型发送数据。
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 BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.