null のリストをテストしています。見つけるたびに、それを配列に保存して検証メッセージに実装します。
必要な出力は次のようになります。
フィールド 1 は必須
フィールド 4 は必須
など...
しかし、私は新しい行を開始することができないようです.
これで、次のようになります。
フィールド 1 は必須です フィールド 4 は必須です
これを達成する方法を知っている人はいますか?
編集:
コントローラ:
IDictionary<int, String> emptyFields = new Dictionary<int, String>();
foreach (Something thing in AnotherThing.Collection)
{
if (thing.Property == null)
emptyFields.add(thing.Index, thing.Name);
}
if (emptyFields.Any())
throw new CustomException() { EmptyFields = emptyFields };
この例外は次の場所で処理されます。
catch (CustomException ex)
{
ModelState.AddModelError("file", ex.GetExceptionString());
return View("theView");
}
カスタム例外:
public class CustomException: Exception
{
public IDictionary<int,String> EmptyFields { get; set; }
public override String Label { get { return "someLabel"; } }
public override String GetExceptionString()
{
String msg = "";
foreach (KeyValuePair<int,String> elem in EmptyFields)
{
msg += "row: " + (elem.Key + 1).ToString() + " column: " + elem.Value + "<br/>";
}
return msg;
}
}
見る:
<span style="color: #FF0000">@Html.Raw(Html.ValidationMessage("file").ToString())</span>