Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MVC(Razor)助手类(ValidationImage)未呈现/工作?_Image_Validation_Asp.net Mvc 3_Razor - Fatal编程技术网

MVC(Razor)助手类(ValidationImage)未呈现/工作?

MVC(Razor)助手类(ValidationImage)未呈现/工作?,image,validation,asp.net-mvc-3,razor,Image,Validation,Asp.net Mvc 3,Razor,我正在尝试使用以下帮助器类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.CompilerServices; using System.Web.Mvc; using System.Web.Routing; namespace myNamespace { public static class ValidationHelp

我正在尝试使用以下帮助器类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.CompilerServices;
using System.Web.Mvc;
using System.Web.Routing;

namespace myNamespace
{
    public static class ValidationHelper
    {

        public static MvcHtmlString ValidationImage(this HtmlHelper helper, string name)
        {
            if (helper.ViewData.ModelState[name] == null || helper.ViewData.ModelState[name].Errors == null || helper.ViewData.ModelState[name].Errors.Count == 0)
            {
                return MvcHtmlString.Empty;
            }
                var tag = new TagBuilder("img");
                tag.Attributes.Add("src", "../Content/Images/check_bg_invalid.png");
                tag.Attributes.Add("alt", helper.ViewData.ModelState[name].Errors[0].ErrorMessage);
                tag.Attributes.Add("title", helper.ViewData.ModelState[name].Errors[0].ErrorMessage);
                tag.Attributes.Add("class", HtmlHelper.ValidationMessageCssClassName);
                return MvcHtmlString.Create(tag.ToString(TagRenderMode.SelfClosing));
        }
    }
}
从一个角度来看:

@Html.ValidationImage("FirstName")
尝试在出现验证错误时启动映像

因此,我有我的:

@Html.TextBox("FirstName", null, new { @class = "my_input" })
在同一视图中(都在@Html.BeginForm中)

这是我的model.cs课程:

 public class QuoteModel
    {
        [Required(ErrorMessage = "You must specify a First Name.")]
        public string First Name { get; set; }

    }
public ActionResult Quote()
{
    //return View();
    var model = new QuoteModel();
    return View(model);
}

[HttpPost]
public ActionResult Quote(QuoteModel model)
{
    if (ModelState.IsValid)
    {
        try
        {
        }
        catch (Exception)
        {
            return View(model);
        }
    }
    return RedirectToAction("QuoteSuccess");
}

public ViewResult QuoteSuccess()
{
    return View();
}
这是我的controller.cs类:

 public class QuoteModel
    {
        [Required(ErrorMessage = "You must specify a First Name.")]
        public string First Name { get; set; }

    }
public ActionResult Quote()
{
    //return View();
    var model = new QuoteModel();
    return View(model);
}

[HttpPost]
public ActionResult Quote(QuoteModel model)
{
    if (ModelState.IsValid)
    {
        try
        {
        }
        catch (Exception)
        {
            return View(model);
        }
    }
    return RedirectToAction("QuoteSuccess");
}

public ViewResult QuoteSuccess()
{
    return View();
}
请记住,我可能在前面的所有代码块中都打错了

原来我用

@Html.ValidationMessage("FirstName") 
单击“发送”后将触发错误消息。我现在尝试使用上面的帮助程序进行ValidationImage,但是没有触发任何操作(并且错误消息没有显示)。也不会输出任何带有

请注意,我使用了一个助手,因此不需要“.js”扩展-这在过去是有效的,并显示在ViewSource中。所以没有问题

最后,我确实设置了web.config:

@{ Html.EnableClientValidation(); }


非常感谢您对我的问题(特别是主要问题)的任何想法。

在我看来,只有在服务器端验证失败时,您的验证映像才会输出到页面中。这意味着您的客户端验证可能无法显示图像,因为它不存在于HTML标记中(
返回MvcHtmlString.Empty;

如果希望验证在客户端工作(并显示图像),可能需要做一些工作来连接到现有的验证。这里有一篇关于自定义验证的文章:


虽然它可能有点过于复杂-可能有一种更简单的方法来连接它(尽管我现在找不到)。

我不想问,但是您是否在
ValidationImage
方法中设置了断点来检查传入数据的值?(例如您的Viewstate数据“”)有如此多的视图,您可能会认为您的人会对问题/答案进行投票: