【解決方法】C#で改行を追加するにはどうすればよいですか


構築中の Web フォームの背後にいくつかのロジックがあります。 このロジックは、ユーザーが必須フィールドに有効な値を入力したかどうかを処理します。 以下のコード:

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 は改行とほとんどの空白を無視します。 メッセージを強制的に複数行に表示するには、次のようにする必要があります。 ある <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";
         }

Richards が私の解決策に最も近かったと信じていますが、違いもありました。 ご回答ありがとうございます!

コメント

タイトルとURLをコピーしました