当jQuery更新@ Html.TextBoxFor时,如何刷新@ Html.ValidationMessageFor?

时间:2018-09-13 03:59:03

标签: c# jquery html asp.net-mvc

我正在通过ASP.NET-MVC开发Web应用程序。

型号:

[Display(Name = "REGISTERED ADDRESS")]
[StringLength(255)]
public string ADDRESS { get; set; }

HTML:

@Html.TextBox("textBoxFillToAnothertextBox", null, new { id = "textBoxFillToAnothertextBox", @class = "form-control", placeholder = "Fill in to apply for textBoxAddress" })
@Html.TextBoxFor(m => m.ADDRESS, new { id = "textBoxAddress" + i, @class = "form-control" })
@Html.ValidationMessageFor(m => m.ADDRESS, "", new { @class = "text-danger" })

jQuery:

$('#textBoxFillToAnothertextBox').on('keyup change', function () {
    $('#textBoxAddress').val($('#textBoxFillToAnothertextBox').val());
});

情况1:我将文本填充到“ textBoxAddress”。文本长度超过255,然后正常显示“ ValidationMessage of Address”。

情况2:我将文本填充到由jQuery设置为“ textBoxAddress”的“ textBoxFillToAnothertextBox”。文本长度超过255,则不会显示“ ValidationMessage of Address”。

在第二种情况下,有人可以帮我推荐吗?

2 个答案:

答案 0 :(得分:2)

使用maxlength属性:

 @Html.TextBox("textBoxFillToAnothertextBox", null, new { id = "textBoxFillToAnothertextBox",maxlength = 250 , @class = "form-control", placeholder = "Fill in to apply for textBoxAddress" })

$('#textBoxFillToAnothertextBox').on('keyup change', function() {
    if ($('#textBoxFillToAnothertextBox').val().length > 250) {
        $('#ADDRESS').text('error message');
    } else {
        $('#textBoxAddress').val($('#textBoxFillToAnothertextBox').val());
    }
});

答案 1 :(得分:0)

目前,我使用@StephenMuecke中的概念。

jQuery:

#include <iostream>
using namespace std;
int main(void)
{
    int x;
    int count = 0;
    int N;
    double sum = 0;
    double average;
    char ans = 'Y';

        cout << "Enter number of values, N, to be read in <Enter>:" << endl;
        cin >> N;

        do
        {
            if(N > 0){ //THIS IF STATEMENT WILL ALWAYS RUN AT LEAST ONCE
            cout << "\n Enter a grade <Enter>: ";
            cin >> x;
            sum = sum + x;
            count++;
            }

            if(count == N && N != 0) {//THIS IF STATEMENT WILL ALSO ALWAYS RUN AT LEAST ONCE
                average = average = sum / N;
                cout << "The average of these " << N << " grades is " << average << endl;
                cout << "Would you like to enter more values to calculate your grade average?\n";
                    cin>>ans;
                    if(ans == 'Y') {//This one depends on it's parents result.
                        x = 0;
                        N = 0;
                        sum = 0;
                        count = 0;
                        cout << "Enter number of values, N, to be read in <Enter>:" << endl;
                        cin >> N;
                    }
            }

        } while (ans == 'Y' && N != 0);

        if (N == 0)
            cout << "You have entered 0 numbers. No average will be computed. Bye! \n";

        system("pause");
        return 0;
}

这对我有用。