mvc4 razor渲染验证问题

时间:2013-09-20 15:00:51

标签: 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; }

    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();
}

在我的本地计算机上呈现如下:

<form action="/validatie/quoterequest" method="post">

<input class="span6" data-val="true" data-val-required="Adres is een verplicht veld"     id="D_Address" name="D_Address" type="text" value="" />

<input class="span2" data-val="true" data-val-number="The field D_AddressNumber must be a number." data-val-required="Huisnummer is verplicht" id="D_AddressNumber" name="D_AddressNumber" type="text" value="" />

<input class="span2" id="D_AddressExtra" name="D_AddressExtra" type="text" value="" /><br />
<span class="field-validation-valid label label-important" data-valmsg-for="D_Address" data-valmsg-replace="true"></span><span class="field-validation-valid label label-important" data-valmsg-for="D_AddressNumber" data-valmsg-replace="true"></span>    <br />

<input class="span4" data-val="true" data-val-required="Postcode is een verplicht veld" id="D_Zipcode" name="D_Zipcode" type="text" value="" /><span class="field-validation-valid label label-important" data-valmsg-for="D_Zipcode" data-valmsg-replace="true"></span>    <br />
<input class="span4" data-val="true" data-val-required="Plaats is een verpicht veld" id="D_City" name="D_City" type="text" value="" /><span class="field-validation-valid label label-important" data-valmsg-for="D_City" data-valmsg-replace="true"></span>    <br />

<input id="Submit1" type="submit" value="Versturen" class="btn btn-primary" /><br />
<div class="validation-summary-valid" data-valmsg-summary="true"><ul>
<li style="display:none"></li>
</ul></div>

</form>

在我的服务器上:

<form action="/Validatie/Quoterequest" method="post">

<input class="span6" id="D_Address" name="D_Address" type="text" value="" />

<input class="span2" data-val="true" data-val-number="The field D_AddressNumber must be a number." data-val-required="Huisnummer is verplicht" id="D_AddressNumber" name="D_AddressNumber" type="text" value="" />

<input class="span2" id="D_AddressExtra" name="D_AddressExtra" type="text" value="" />    <br /> <span class="field-validation-valid label label-important" data-valmsg-for="D_Address" data-valmsg-replace="true"></span><span class="field-validation-valid label label-important" data-valmsg-for="D_AddressNumber" data-valmsg-replace="true"></span>    <br /> 

<input class="span4" id="D_Zipcode" name="D_Zipcode" type="text" value="" /><span class="field-validation-valid label label-important" data-valmsg-for="D_Zipcode" data-valmsg-replace="true"></span>    <br /> 

<input class="span4" id="D_City" name="D_City" type="text" value="" /><span class="field-validation-valid label label-important" data-valmsg-for="D_City" data-valmsg-replace="true"></span>    <br />     

<input id="Submit1" type="submit" value="Versturen" class="btn btn-primary" />     <br /> 
<div class="validation-summary-valid" data-valmsg-summary="true"><ul><li style="display:none"></li> </ul></div>

</form> 

我也有这方面的信息 本地机器 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

在服务器上IISVersion : C:\Windows\Microsoft.NET\Framework\v4.0.30319\

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

1 个答案:

答案 0 :(得分:0)

我在VS2010的Windows7 x64 MVC4中遇到过类似的症状。即使在简单的表单中,代码也不呈现data-val属性。

我现在已经解决了!

要进行调试,首先尝试使用您的模型和添加&gt;视图菜单选项创建强类型的“编辑”视图。查看您期望的属性是否显示在创建的视图中。如果没有,和我一样,你发现没有自动添加字段,你的问题就是模型定义。

你可能有类似......

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

现在,重新使用您的解决方案并再次尝试添加&gt;查看。请记住,这可以使用Reflection,因此需要将其编译到DLL中。如果这样可行,您还会发现现在从EditorFor方法中获取data-val属性。

鉴于您正在将DLL复制到服务器环境,您的源代码是否实际代表您认为正在部署的已编译DLL?我将重新编译Debug和Release版本,在本地测试,然后将发行版本复制到服务器。

-

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

我最初认为它是jQuery版本或事件问题,所以做了以下mods和检查:
1)web.config - ClientVlidationEnabled和UnobtrusiveJavascriptEnabled都为真。
2)这些属性位于正确的web.config文件中 - 最外面的一个 3)简化视图以避免使用捆绑包 - 捆绑包可以重新排序文件
4)将jQuery降级为MVC4随附的默认版本 5)避免使用partials的简化形式 - partials可能缺少一个导致相同问题的formContext 6)在_Layout.cshtml文件中以该顺序显式引用jQuery,jQuery.validate和jQuery.validate.unobtrusive。
7)在FireBug中检查呈现的HTML,而不依赖于浏览器中的查看源

我的项目是对MVC4的MVC3升级,因此我将所有自定义文件导入到原始MVC4项目中,以确保我没有遗漏任何内容。我确信答案在于隔离为什么dataFor方法不能通过设计呈现data-val属性。代码评论说:

// 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方法。因此,作为一种解决方法,您可以将“帮助”方法的输出手动输入到视图中,并节省大量时间。

祝你好运!