【问题标题】:ASP.NET MVC unobtrusive client validation with multiple models具有多个模型的 ASP.NET MVC 不显眼的客户端验证
【发布时间】:2011-08-01 01:18:14
【问题描述】:

ThingController 创建一个模型,其中包含(以及其他属性)事物的集合。这些可以像这样在视图中进行编辑:

<form action="@Url.Action("Update", "Thing")" method="post">        
    <table>
        <tr>
            <th>Foo</th>
            <th>Bar</th>
        </tr>        
            @foreach (var thing in ViewData.Model.Things)
            {                
                <tr class="raw-data">
                    <td class="raw-data"><input name="things[@rowCount].Foo" class="raw-data" readonly="readonly" type="text" value="@thing.Foo" /></td>
                    <td class="raw-data"><input name="things[@rowCount].Bar" class="raw-data" type="text" value="@thing.Bar" /></td>
                </tr>                
                rowCount++;
            }
    </table>

    <br />
    <input type="submit" value="OK" />      
</form>

控制器包含以下动作,允许同时更新多个事物:

public ActionResult Update(ThingModel[] things)
{
  ...
}    

我在 Thing 类的属性中添加了一些验证属性:

[Required]
[Range(0, 500000, ErrorMessage = "Foo must be within 0 and 500,000.")]
public double Foo{ get; set; }

[Required]
[Range(0, 500000, ErrorMessage = "Bar must be within 0 and 500,000.")]
public double Bar { get; set; }

问题是,我不知道如何使用 TextBoxFor 助手等添加不显眼的验证。

在这一点上,我认为正确的方法是使用验证属性手动标记输入字段,但我想知道是否有人可以向我指出一些文档、教程等,这些文档、教程等演示了帮助程序、多个模型的使用不显眼的验证?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3


    【解决方案1】:

    我有一个类似的问题,用户可以动态地将多封电子邮件添加到他们的帐户中。 我通过手动添加验证来使用 Jquery 修复它。您应该为您的表单命名,并将验证添加到您的所有项目。我的问题应该是这样的:

        $('#frmYourForm').validate();
        for (var i = 0; i < 'CountOfAllFields'; i++) {
            $('#Things_' + i + '__Foo').rules('add', { required: true, messages: { required: 'The Foo field is required'} });
            $('#Things_' + i + '__Bar').rules('add', { required: true, messages: { required: 'The Bar field is required'} });
        }
    

    根据我的记忆输入,所以如果我写错了请不要开枪。我不知道 Range 事物的确切语法,但您应该查看 Jquery.validate 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-19
      • 2012-04-15
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多