如何在C#中添加换行符

[ad_1]

我正在构建的网络表单背后有一些逻辑。 该逻辑处理用户是否为必填字段输入了有效值。 代码如下:

C#
var operationResult = new BO.OperationResult();

         // Physical Location Required
         if (physicalLocationDesc.Trim().Length <= 0)
         {
             operationResult.Successful = false;
             operationResult.Message += "A location is required as part of the entry";
         }
         // Type of Connection Desc. Required
         if (typeOfConnectionID.Trim().Length <= 0)
         {
             operationResult.Successful = false;
             operationResult.Message += "A description for the connection type is required.";
         }
         // Password change frequency desc. required
         if (passwordChangeFrequency.Trim().Length <= 0)
         {
             operationResult.Successful = false;
             operationResult.Message += "Please enter a description for the Password Change Frequency";
         }

我遇到的问题是 operationResult.Message 返回:

“条目中需要提供位置需要连接类型的描述。请输入密码更改频率的描述”

我想在每个单独的错误消息后添加换行符。 关于如何解决这个问题有什么想法吗? (请注意,如果未输入 1 个字段,上面的代码可以正常工作,但如果未输入 2 个或更多字段,则代码将变得难以阅读。)

解决方案1

解决方案2

解决方案5

两种方式:
1. 尽可能长时间地将各个消息分开,然后在“末尾”将它们合并一次:

C#
var operationResult = new BO.OperationResult();
 List<string> messages = new List<string>();
// Physical Location Required
if (string.IsNullOrWhiteSpace(physicalLocationDesc))
{
  operationResult.Successful = false;
  messages.Add("A location is required as part of the entry");
}
// Type of Connection Desc. Required
if (string.IsNullOrWhiteSpace(typeOfConnectionID))
{
  operationResult.Successful = false;
  messages.Add("A description for the connection type is required.");
}
// Password change frequency desc. required
if (string.IsNullOrWhiteSpace(passwordChangeFrequency))
{
  operationResult.Successful = false;
  messages.Add("Please enter a description for the Password Change Frequency");
}
// etc...
operationResult.Message = string.Join(Environment.NewLine, messages);

2. 使用 StringBuilder

C#
var operationResult = new BO.OperationResult();
 StringBuilder messages = new StringBuilder();
string sep = string.Empty;
// Physical Location Required
if (string.IsNullOrWhiteSpace(physicalLocationDesc))
{
  operationResult.Successful = false;
  messages.Append(sep).Append("A location is required as part of the entry");
  sep = Environment.NewLine;
}
// Type of Connection Desc. Required
if (string.IsNullOrWhiteSpace(typeOfConnectionID))
{
  operationResult.Successful = false;
  messages.Append(sep).Append("A description for the connection type is required.");
  sep = Environment.NewLine;
}
// Password change frequency desc. required
if (string.IsNullOrWhiteSpace(passwordChangeFrequency))
{
  operationResult.Successful = false;
  messages.Append(sep).Append("Please enter a description for the Password Change Frequency");
  sep = Environment.NewLine;
}
// etc...
operationResult.Message = messages.ToString();

解决方案6

之前的解决方案遗漏的是注释 “网络表格”。 这表明您的消息正在输出到 HTML 文档。

HTML 会忽略换行符和大部分空白。 要强制消息显示在多行中,您需要使用 A <br> 标签[^]。

C#
var operationResult = new BO.OperationResult();
var messages = new List<string>(3);

// Physical Location Required
if (physicalLocationDesc.Trim().Length <= 0)
{
    operationResult.Successful = false;
    messages.Add("A location is required as part of the entry");
}
// Type of Connection Desc. Required
if (typeOfConnectionID.Trim().Length <= 0)
{
    operationResult.Successful = false;
    messages.Add("A description for the connection type is required.");
}
// Password change frequency desc. required
if (passwordChangeFrequency.Trim().Length <= 0)
{
    operationResult.Successful = false;
    messages.Add("Please enter a description for the Password Change Frequency");
}

if (messages.Count != 0)
{
    operationResult.Message = string.Join("<br>", messages);
}

解决方案7

尽管我发布这个问题已经有一段时间了。 我想我会发布我的解决方案以供将来使用。

您可以添加“

” 引号内的语法。作为示例,我发布了适合我的情况的确切代码。

C#
var operationResult = new BO.OperationResult();
         // Physical Location Required
         if (physicalLocationDesc.Trim().Length <= 0)
         {
             operationResult.Successful = false;
             operationResult.Message += " <br /> <br /> A location is required as part of the entry";
         }
         // Type of Connection Desc. Required
         if (typeOfConnectionID.Trim().Length <= 0)
         {
             operationResult.Successful = false;
             operationResult.Message += "<br /> <br />A description for the connection type is required.";
         }
         // Password change frequency desc. required
         if (passwordChangeFrequency.Trim().Length <= 0)
         {
             operationResult.Successful = false;
             operationResult.Message += "<br /><br />Please enter a description for the Password Change Frequency";
         }

我相信理查兹是最接近我的解决方案,但也存在差异。 感谢您的回复!

[ad_2]

コメント

标题和URL已复制