尽管存在模型错误,但MVC3 Ajax验证未显示 [英] MVC3 Ajax validation not showing despite model errors

查看:72
本文介绍了尽管存在模型错误,但MVC3 Ajax验证未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使验证摘要以ajax形式显示.

I have problems getting the validation summary to show in an ajax form.

Web.Config

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)

ViewModel:

ViewModel:

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
    }
}

  1. 如果我运行ModelState.AddModelError("err","eerrr");它将显示摘要
  2. 如果我进行调试,则ModelState会为每个"NewFileName"包含错误消息

以某种方式,我的自定义HTML列表中不存在错误消息.

Somehow the errormessage doesnt exist in my custom Html-list.

如何将其添加到此自定义单选按钮+文本框列表中?

How do I add it to this custom listning of radiobuttons + textbox?

推荐答案

您还应该添加

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>

这是关于Unobtrusive的非常好的参考ASP.NET MVC 3中的客户端验证.

This is a very nice reference on Unobtrusive Client Validation in ASP.NET MVC 3.

这篇关于尽管存在模型错误,但MVC3 Ajax验证未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆