以下代码永远不会在我的网页上呈现(ViewData["ERRORS"]
不为空),我不知道为什么......
@section scripts{
<script>
$(document).ready(function () {
$('.saveButton').click(function () {
$('#frmMain').submit();
});
@if (ViewData["ERRORS"] != null)
{
foreach (KeyValuePair<string, string> item in (Dictionary<string,string>)ViewData["ERRORS"])
{
<text>$("#@item.Key").addClass("has-error");$("#__count_@item.Key").val("@item.Value");</text>
}
}
});
</script>
}
生成ViewData
的控制器代码:
Dictionary<string, string> errors = new Dictionary<string, string>();
#region electric
if(form["__count_ht__"] != null && !string.IsNullOrEmpty(form["__count_ht__"]))
{
float.TryParse(form["__count_ht__"], out ht);
if(ht <= 0)
{
errors.Add("ht", form["__count_ht__"]);
}
}
if (form["__count_nt__"] != null && !string.IsNullOrEmpty(form["__count_nt__"]))
{
float.TryParse(form["__count_nt__"], out nt);
if(nt <= 0)
{
errors.Add("nt", form["__count_nt__"]);
}
}
#endregion
if(errors.Count == 0)
{
}else
{
ViewData["ERRORS"] = errors;
}
return View();
}