Asp.net mvc 4 mvc4 razor渲染验证问题

Asp.net mvc 4 mvc4 razor渲染验证问题,asp.net-mvc-4,razor,asp.net-mvc-validation,Asp.net Mvc 4,Razor,Asp.net Mvc Validation,验证呈现问题 我的模型中有以下数据注释: [Required(ErrorMessage = "Adres is een verplicht veld")] [MaxLength(250)] public string D_Address { get; set; } [Required(ErrorMessage = "Huisnummer is verplicht")] public int D_AddressNumber { get; set; }

验证呈现问题

我的模型中有以下数据注释:

    [Required(ErrorMessage = "Adres is een verplicht veld")]
    [MaxLength(250)]
    public string D_Address { get; set; }

    [Required(ErrorMessage = "Huisnummer is verplicht")]
    public int D_AddressNumber { get; set; }

    public string D_AddressExtra { get; set; }

    [Required]
    public int CapacityRate { get; set; }

    [Required(ErrorMessage = "Postcode is een verplicht veld")]
    [MaxLength(35)]
    public string D_Zipcode { get; set; }

    [Required(ErrorMessage = "Plaats is een verpicht veld")]
    [MaxLength(250)]
    public string D_City { get; set; }
在我的cshtml中:

@using (Html.BeginForm())
{ 
    @Html.TextBoxFor(m => m.D_Address, new { @class = "span6" })
    @Html.TextBoxFor(m => m.D_AddressNumber, new { @class = "span2" })
    @Html.TextBoxFor(m => m.D_AddressExtra, new { @class = "span2" })
    <br />
    @Html.ValidationMessageFor(m => m.D_Address, null, new { @class = "label label-important" })
    @Html.ValidationMessageFor(m => m.D_AddressNumber, null, new { @class = "label label-important" })
    <br />
    @Html.TextBoxFor(m => m.D_Zipcode, new { @class = "span4" })
    @Html.ValidationMessageFor(m => m.D_Zipcode, null, new { @class = "label label-important" })
    <br />
    @Html.TextBoxFor(m => m.D_City, new { @class = "span4" })
    @Html.ValidationMessageFor(m => m.D_City, null, new { @class = "label label-important" })
    <br />
    <input id="Submit1" type="submit" value="Versturen" class="btn btn-primary" />
    <br />
    @Html.ValidationSummary();
}
在服务器上
IISVersion:C:\Windows\Microsoft.NET\Framework\v4.0.30319\

 ProcesVersionInfo : File: C:\Program Files (x86)\IIS Express\iisexpress.exe InternalName: iisexpress.exe 
OriginalFilename: iisexpress.exe.mui 
FileVersion: 8.0.8418.0 (winmain(eokim).120521-1311) 



FileDescription: IIS Express Worker Process
 Product: Internet Information Services 
ProductVersion: 8.0.8418.0 
Debug: False
 Patched: False 
PreRelease: False 
PrivateBuild: True 
SpecialBuild: False 
Language: Language Neutral
ProcesVersionInfo : File: C:\Windows\SysWOW64\inetsrv\w3wp.exe 
InternalName: w3wp.exe OriginalFilename: w3wp.exe.mui 
FileVersion: 7.5.7600.16385 (win7_rtm.090713-1255) 
FileDescription: IIS Worker Process Product: Internet Information Services ProductVersion: 7.5.7600.16385
 Debug: False
 Patched: False
 PreRelease: False 
PrivateBuild: False 
SpecialBuild: False 
Language: Taalonafhankelijk

我在VS2010的Windows7 x64 MVC4中也经历过类似的症状。代码甚至没有以简单的形式呈现数据val属性

我现在已经解决了

要进行调试,请首先尝试使用模型和“添加”>“视图”菜单选项创建强类型的“编辑”视图。查看所创建的视图中是否显示所需的属性。如果没有,并且像我一样,您发现没有自动添加字段,那么您的问题是模型定义

你可能有点像

public class MyModel 
{
     [Required(ErrorMessage = "or else!")]

     public string My_Broken_DataValEmittingProperty;
}
您需要将其修改为:

public class MyModel 
{
     [Required(ErrorMessage = "or else!")]

     public string My_Working_DataValEmittingProperty { get; set; }
}
现在,重建您的解决方案并再次尝试添加>视图。请记住,这是使用反射工作的,因此需要将其编译到DLL中。如果这样做有效,您还将发现现在可以从EditorFor方法获取数据val属性

假设您正在将DLL复制到服务器环境,那么您的源代码是否实际表示您认为正在部署的已编译DLL?我将重新编译调试版本和发布版本,在本地进行测试,然后将发布版本复制到服务器

--

尽管如此,我所经历的过程可能会帮助您进一步调试。

我最初认为这是jQuery版本控制或事件问题,因此进行了以下修改和检查:
1) web.config-ClientVlidationEnabled和UnobtrusiveJavascriptEnabled均为true。
2) 这些属性位于正确的web.config文件中—最外面的一个
3) 简化视图以避免完全使用捆绑包-捆绑包可以对文件重新排序
4) 将jQuery降级为MVC4附带的默认版本
5) 简化表单以避免使用partials-partials可能缺少formContext,从而导致相同的问题
6) 在_Layout.cshtml文件中以该顺序显式引用jQuery、jQuery.validate和jQuery.validate.unobtrusive。
7) 检查FireBug中呈现的HTML,不依赖于在浏览器中查看源代码

我的项目是MVC3到MVC4的升级,所以我将所有自定义文件导入到一个virgin MVC4项目中,以确保没有遗漏任何内容。我确信答案在于隔离为什么数据val属性不是通过EditorFor方法中的设计呈现的。代码注释说:

// Only render attributes if unobtrusive client-side validation is enabled, and then
// only if we've never rendered validation for a field with this name in this form. 
// Also, if there's no form context, then we can't render the attributes (we'd have no
// <form> to attach them to).
//仅当启用了不引人注目的客户端验证时才呈现属性,然后
//仅当我们从未在此表单中对具有此名称的字段进行过验证时。
//此外,如果没有表单上下文,则无法呈现属性(我们没有
//将它们连接到)。
请记住,这些是辅助方法。因此,作为一种解决方法,您可以手动将“helper”方法的输出输入到视图中,从而节省大量时间

祝你好运

public class MyModel 
{
     [Required(ErrorMessage = "or else!")]

     public string My_Working_DataValEmittingProperty { get; set; }
}
// Only render attributes if unobtrusive client-side validation is enabled, and then
// only if we've never rendered validation for a field with this name in this form. 
// Also, if there's no form context, then we can't render the attributes (we'd have no
// <form> to attach them to).