【问题标题】:validate model on change of texbox event验证文本框事件更改的模型
【发布时间】:2014-03-24 13:04:54
【问题描述】:

我有一个模型类

public class ActorPhoneModel
    {
        public int PhoneTypeTd { get; set; }

        public string PhoneType { get; set; }

        [StringLength(30, ErrorMessageResourceType = typeof(ErrorMessage),
            ErrorMessageResourceName = "phone_number_you_entered_must_be_at_least_five_characters", ErrorMessage = "", MinimumLength = 5)]

        [RegularExpression(@"/^[ 0-9()+-]*$/", ErrorMessageResourceType = typeof(ErrorMessage),
            ErrorMessageResourceName = "phone_number_is_invalid", ErrorMessage = "")]
        public string PhoneNumber { get; set; }
    }

并在视图中

@foreach (var phone in Model.Phones)
                {
                    <tr>
                        <td>@phone .PhoneType</td>
                        <td>
                            @Html.TextBoxFor(m => phone .PhoneNumber, new
                       {
                           @class = "phoneNumber form-control",
                           data_phoneType = @phone .PhoneTypeTd,
                           data_actorId = Model.ActorId,
                           maxlength = "30"
                       })

                        </td>
                    </tr>
                }

我正在使用 ajax post 来保存更改的电话号码。 在文本框的文本更改事件中,我想验证模型

        $(".phoneNumber").change(function () {
            var phoneType = $(this).attr("data-phoneType");
            var actorId = $(this).attr("data-actorId");
            var phoneNumber = $(this).val();

            if (//validate model) {

                //code to save phone number of that phoneType
            }
        });

如何在 jquery 中验证该模型。我已经尝试过 form.valid() 方法,但它对我不起作用。

【问题讨论】:

    标签: jquery ajax asp.net-mvc data-annotations razor-2


    【解决方案1】:

    我猜你只需要通过 jQuery 进行验证, 因为在更改文本时没有服务器端调用。 而且您只需要验证一个可以使用 jQuery 的 RegularExpression 的字段。 会简单很多。 希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多