6

検証にDataAnnotationを使用していますが、エラーメッセージ(データ注釈)を表示するのではなく、ポップアップ(ダイアログ/アラート)で表示したいのですが...このリンクを使用してコードを実装しました。

プロジェクトテンプレートはモバイルです何かが足りない場合は教えてください?? http://forums.asp.net/t/1738076.aspx/1

Javascript:-

$('#Test').bind('invalid-form.validate', function (form, validator) {
    alert('InsideTest');   
     var $list = $('#errorlist ul:first')
     if ($list.length && validator.errorList.length) {
            $list.empty();
            $.each(validator.errorList, function () {
                $("<li />").html(this.message).appendTo(list);
            });
            $list.dialog({
                title: 'Please correct following errors:',
            });
 }
});
Forgot to add html...    

About.cshtml:-

@model WRDSMobile.Models.Test

<div id="errorlist" style="display:none"><ul></ul></div>    
@using (Html.BeginForm(null, null, FormMethod.Post, new { name = "Test", id = "Test" }))
{
     @Html.ValidationSummary(true)
     <fieldset>

        <legend>Test</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Age)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Age)
            @Html.ValidationMessageFor(model => model.Age)
        </div>  
            <input type="submit" value="Create" />
    </fieldset>
}
4

3 に答える 3

1

あなたの質問を理解しているかどうかはわかりませんが、コントローラーに ajax 投稿して部分ビューを返すことができます。次に、部分ビューを html 要素にロードし、ダイアログ ボックスにポップアップ表示します。

 [HttpPost]
    public ActionResult validate(string firstName,string lastName){

        //logic here for validation
         return PartialView();
    }


$.ajax({
            type: "POST",
            data: {firstName: nameVal, lastName: lastNameVal },
            url: "/myController/validate",
            dataType: "html",
            success: function (data) {
                if (data) {
                    var dialog = $('<div></div>');
                    dialog.html(data);
                    dialog.dialog({
                        resizable: false,
                        modal: true,
                        overflow: false,
                        maxWidth: 1200,
                        maxHeight: 600,
                        width: 1200,
                        height: 600,
                        border: 0,
                        buttons: {
                            "Cancel": function () { //cancel
                                $(this).dialog("close");
                            }
                        }

                    });
                }
                else {
                    alert("error");

                }
            }
        });
于 2013-03-12T23:18:22.390 に答える
1

次の CSS/JS を参照として以下に追加します。 min.js

$(document).ready(function () {
        $('#Test').bind('invalid-form.validate', function (form, validator) {
            var $list = $('<ul />')
            if (validator.errorList.length) {
                $.each(validator.errorList, function (i, entity) {
                    $("<li />").html(entity.message).appendTo($list);
                });
                msgbox('Please correct following errors:', $('<div />').append($list));
                return false;
            }
        });
    });

    function msgbox(_title, _messageHtml) {
        $('<div></div>').appendTo('body')
                    .html(_messageHtml)
                    .dialog({
                        modal: true, title: _title, zIndex: 10000, autoOpen: true,
                        width: 'auto', resizable: false,
                        buttons: {
                            Ok: function () {
                                // $(obj).removeAttr('onclick');                                
                                // $(obj).parents('.Parent').remove();

                                $(this).dialog("close");
                            }
                        },
                        close: function (event, ui) {
                            $(this).remove();
                        }
                    });
    };
于 2014-01-03T14:08:21.280 に答える