我已经得到验证摘要在Ajax形式展现的问题。
Web.Config中
<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( “犯错”, “eerrr”); 它会显示总结
- 如果我调试,ModelState中包含了每个“NewFileName” errormessages
不知怎的,errormessage的犯规在我的自定义HTML列表存在。
我该如何将它添加到单选按钮+文本框的这个自定义listning?