【发布时间】:2012-10-06 05:41:38
【问题描述】:
我正在测试 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>
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-3 newline line-breaks validationmessage