Làm cách nào để thêm ngắt dòng trong C#

lập trình


Tôi có một số logic đằng sau biểu mẫu web mà tôi đang xây dựng. Logic xử lý xem người dùng có nhập giá trị hợp lệ cho trường bắt buộc hay không. Mã dưới đây:

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";
         }

Vấn đề tôi gặp phải là OperationResult.Message đang quay trở lại:

“Vị trí được yêu cầu như một phần của mục nhập. Cần phải có mô tả cho loại kết nối. Vui lòng nhập mô tả cho Tần suất thay đổi mật khẩu”

Tôi muốn thêm ngắt dòng sau mỗi thông báo lỗi riêng lẻ. Có ý tưởng nào về cách thực hiện việc này không? (Xin lưu ý rằng mã ở trên hoạt động tốt nếu không nhập 1 trường nhưng trở nên khó đọc hơn nếu không nhập 2 trường trở lên.)

Giải pháp 1

Giải pháp 2

Giải pháp 5

Hai lối:
1. Giữ từng tin nhắn riêng biệt càng lâu càng tốt và sau đó kết hợp chúng một lần ở “cuối”:

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. Sử dụng một 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();

Giải pháp 6

Những giải pháp trước đó đã bỏ lỡ là nhận xét “một biểu mẫu web”. Điều đó cho thấy rằng tin nhắn của bạn đang được xuất ra tài liệu HTML.

HTML bỏ qua ngắt dòng và hầu hết khoảng trắng. Để buộc thông báo xuất hiện trên nhiều dòng, bạn sẽ cần sử dụng Một <br> nhãn[^].

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);
}

Giải pháp 7

Mặc dù đã lâu rồi tôi mới đăng câu hỏi này. Tôi nghĩ tôi sẽ đăng giải pháp của mình cho các mục đích trong tương lai.

Bạn có thể thêm “

” cú pháp trong dấu ngoặc kép. Để làm ví dụ, tôi đã đăng mã chính xác phù hợp với trường hợp của mình.

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";
         }

Tôi tin rằng Richards là người gần nhất với giải pháp của tôi nhưng vẫn có những khác biệt. cảm ơn vì đã phản hồi!

コメント

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