我在使用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
}
}
我的自定义Html列表中不存在错误消息。
如何将其添加到radiobuttons + textbox的自定义列表中?
答案 0 :(得分:2)
你还应该添加
jquery.validate.js
按顺序
<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>
对于ASP.NET MVC 3中的不显眼的客户端验证,这是一个非常好的reference。