我无法让验证摘要以 ajax 形式显示。
网络配置
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
局部视图:
@Html.ValidationSummary()
@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "file" }))
控制器:
[HttpPost]
public PartialViewResult Move(List<MoveFileResult> moveFileResult)
{
if (ModelState.IsValid)
{
}
// ModelState.AddModelError("err", "eerrr");
....
呈现的 HTML:
<input checked="checked" id="Replace3665"
name="MoveFileResult[0].MoveFileAction"
type="radio" value="Replace"> Overwrite
<input id="Rename3665"
name="MoveFileResult[0].MoveFileAction"
type="radio" value="Rename">
Rename to:
<input id="MoveFileResult_0__NewFileName"
name="MoveFileResult[0].NewFileName"
type="text" value="">
<span class="field-validation-valid"
data-valmsg-for="MoveFileResult[0].NewFileName"
data-valmsg-replace="true"></span>
脚本:(更新)
<script src="/Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
它们是这样创建的:
@Html.TextBox(String.Format("MoveFileResult[{0}].NewFileName", i), item.NewFileName)
@Html.ValidationMessage(String.Format("MoveFileResult[{0}].NewFileName", i), item.NewFileName)
视图模型:
namespace MyProj.MVC.Areas.Admin.ViewModels
{
public class MoveFilesViewModel
{
public List<MoveFileResult> MoveFileResult { get; set; }
}
public class MoveFileResult
{
[Required]
public int FileID { get; set; }
[Required]
public MoveFileAction MoveFileAction { get; set; }
//[RequiredIf("MoveFileAction", 2)]
[Required]
public string NewFileName { get; set; }
}
public enum MoveFileAction : byte
{
Move = 0,
Replace = 1,
Rename = 2
}
}
- 如果我运行 ModelState.AddModelError("err", "eerrr"); 它将显示摘要
- 如果我调试,ModelState 包含每个“NewFileName”的错误消息
不知何故,错误消息在我的自定义 Html 列表中不存在。
如何将其添加到此单选按钮 + 文本框的自定义列表中?