简体   繁体   English

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

[英]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.

相关问题 如何使验证注释可选? Asp.net核心 - How to make validation annotation optional? Asp.net Core 如何使用Asp.Net MVC数据库中的电子邮件地址在邮政上发送电子邮件 - How to send an email on Postal using an email address from the database Asp.Net MVC 如何在ASP.NET MVC中使用邮政将电子邮件发送到数据库中的多个电子邮件地址 - How to send an email to multiple email addresses in the database using Postal in ASP.NET MVC 如何使用structuremap asp.net mvc注册带有可选参数的可选装饰器或装饰器? - How to register an optional decorator or decorator with optional parameters using structuremap asp.net mvc? 如何使ItemTempleField成为ASP.Net中的ReadOnly字段 - How to make ItemTempleField to ReadOnly Field in ASP.Net 如何使字段唯一ASP.NET MVC5 - How to make a field unique ASP.NET MVC5 具有可选字段和值的ASP.NET MVC查询 - ASP.NET MVC query with optional field and value Asp.net 核心 MVC 在路由中使动作可选 - Asp.net core MVC make action optional in routing 如何在 Asp.Net 中制作可选的 Model Class 属性 Web Z72664DC0959F3B46C04891F8C037 - How to make optional Model Class Properties in Asp.Net Web Api 如何使用 C# 使表单字段“DeviceId”唯一并在重复时抛出错误,ASP.NET MVC 和 Azure Cosmosdb? - How to make form field “ DeviceId” unique and throw error if repeated, ASP.NET MVC and Azure Cosmosdb using C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM