asp.net mvc TextAreaFor没有被验证为必填字段 [英] asp.net mvc TextAreaFor is not getting validated as a required field

查看:223
本文介绍了asp.net mvc TextAreaFor没有被验证为必填字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据输入字段,用于收集笔记.每个便笺都需要便笺数据元素.这是我的模型:

I have a data entry field where I'm gathering notes. The note data element is required for each note. Here's my model:

public interface INoteDataEntryViewModel : IMobilePageDataContract
{
    int CourseId { get; set; }

    [Required(ErrorMessage = @"Note is required")]
    String Note { get; set; }

    [DisplayName(@"Note Date")]
    DateTime NoteDate { get; set; }
}

您可以看到我具有Note属性的Required属性.

You can see that I have the Required attribute for the Note property.

我正在使用Razor显示数据输入表单元素:

I'm using Razor to display the data entry form element:

<div data-role="fieldcontain">
    @Html.LabelFor(m => m.Note)
    @Html.TextAreaFor(m => m.Note)
    @Html.ValidationMessageFor(m => m.Note)
</div>

当我使用"@ Html.TextAreaFor"时,没有对必填字段的验证,因此我可以提交表单.但是,如果我更改为"@ Html.TextBoxFor",则会对必填字段进行验证,并且无法提交表单.关于为何TextAreaFor验证失败的任何想法?我使用的是不显眼的Ajax,是jQueryMobile.

When I use "@Html.TextAreaFor" then there is no validation for the required field and i can submit the form. However, if I change to "@Html.TextBoxFor", then validation happens for the required field and I cannot submit the form. Any ideas on why validation fails for TextAreaFor? I'm using unobtrusive ajax and am jQueryMobile.

感谢您的帮助.

推荐答案

客户端验证不适用于Html.TextAreaFor()帮助器,这是

The client-side validation does not work for the Html.TextAreaFor() helper, here is the related issue reported on Codeplex.

要使其工作,必须用[DataType(DataType.MultilineText)]属性装饰'Note'属性.并且在视图中,使用Html.EditorFor()助手而不是Html.TextAreaFor()助手方法.

To make it work, you have to decorate the 'Note' property with the [DataType(DataType.MultilineText)] attribute. And in the view, use Html.EditorFor() helper instead of the Html.TextAreaFor() helper mehthod.

更新的型号:

public interface INoteDataEntryViewModel : IMobilePageDataContract
{
    int CourseId { get; set; }

    [Required(ErrorMessage = @"Note is required")]
    [DataType(DataType.MultilineText)]
    String Note { get; set; }

    [DisplayName(@"Note Date")]       
    DateTime NoteDate { get; set; }
}

查看:

<div data-role="fieldcontain">
    @Html.LabelFor(m => m.Note)
    @Html.EditorFor(m => m.Note)
    @Html.ValidationMessageFor(m => m.Note)
</div>

这篇关于asp.net mvc TextAreaFor没有被验证为必填字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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