Bagaimana cara menambahkan jeda baris di C#

pemrograman


Saya memiliki logika di balik formulir web yang saya buat. Logikanya menangani apakah pengguna memasukkan nilai yang valid untuk bidang yang wajib diisi. Kode di bawah ini:

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

Masalah yang saya alami adalah OperationResult.Message kembali:

“Lokasi diperlukan sebagai bagian dari entri. Diperlukan deskripsi untuk jenis koneksi. Silakan masukkan deskripsi untuk Frekuensi Perubahan Kata Sandi”

Saya ingin menambahkan jeda baris setelah setiap pesan kesalahan. Adakah ide tentang cara melakukan hal ini? (Harap diperhatikan bahwa kode di atas berfungsi dengan baik jika 1 kolom tidak dimasukkan tetapi menjadi lebih sulit dibaca. 2 kolom atau lebih tidak dimasukkan.)

Solusi 1

Solusi 2

Solusi 5

Dua arah:
1. Pisahkan masing-masing pesan selama mungkin, lalu gabungkan pesan tersebut satu kali di “akhir”:

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. Gunakan a 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();

Solusi 6

Apa yang terlewatkan oleh solusi sebelumnya adalah komentarnya “formulir web”. Itu menunjukkan bahwa pesan Anda dikeluarkan ke dokumen HTML.

HTML mengabaikan jeda baris dan sebagian besar spasi. Untuk memaksa pesan muncul di beberapa baris, Anda harus menggunakan A <br> menandai[^].

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

Solusi 7

Padahal sudah lama sejak saya memposting pertanyaan ini. Saya pikir saya akan memposting solusi saya untuk tujuan masa depan.

Anda dapat menambahkan “

” sintaksis dalam tanda kutip. Sebagai contoh, saya memposting kode persis yang sesuai dengan situasi saya.

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

Saya yakin Richards adalah yang paling dekat dengan solusi saya tetapi ada perbedaan. terima kasih atas tanggapannya!

コメント

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