0

送信されたデータがnull/空のときに検証エラーが表示されない場合の何が問題になっていますか?エラーが表示されます。

コードブロックresult.success == falseで、ビューをリロードするか、jQueryにモデルを無効にするように指示する必要がありますか?

コントローラでのアクションからPostモデルエラーを返すので、クライアント側にあります。

私は今どうすればいい?

最新のjQuery/UI 1.7.2/1.8.20でMVC3.0を使用しています。

<script type="text/javascript">

    $(document).ready(function () {

        // the div holds the html content
        var $dialog = $('<div></div>')
        .dialog({
            autoOpen: false,
            title: 'This is the dialogs title',
            height: 400,
            width: 400,
            modal: true,
            resizable: false,
            hide: "fade",
            show: "fade",
            open: function (event, ui) {
                $(this).load('@Url.Action("Create")');
            },
            buttons: {
                "Save": function () {
                    var form = $('form', this);               

                    $.ajax({
                        url: $(form).attr('action'),
                        type: 'POST',
                        data: form.serialize(),
                        dataType: 'json',
                        success: function (result) {
                           // debugger;
                            if (result.success) {
                                $dialog.dialog("close");
                                // Update UI
                            }
                            else {
                                // Reload the dialog to show model/validation errors 


                            } // else end
                        } // success end
                    }); // Ajax post end

                },
                "Close": function () {
                    $(this).dialog("close");
                }
            } // no comma
        });

        $('#CreateTemplate').click(function () {
            $dialog.dialog('open');
            // prevent the default action, e.g., following a link
            return false;
        });

    });
</script>

私のフォームは:

@using (Html.BeginForm("JsonCreate", "Template")) 
{  
    <p class="editor-label">@Html.LabelFor(model => model.Name)</p> 
    <p class="editor-field">@Html.EditorFor(model => model.Name)</p> 
    <p class="editor-field">@Html.ValidationMessageFor(model => model.Name)</p>     
}

私のコントローラーは:

[HttpGet]
public ActionResult Create()
{          
    return PartialView();
}

[HttpPost]
public ActionResult JsonCreate(Template template)
{
    if (ModelState.IsValid)
    {
        _templateDataProvider.AddTemplate(template);
        // success == true should be asked on client side and then ???
        return Json(new { success = true });
    }

        // return the same view with model when errors exist
        return PartialView(template);
}

動作バージョンは次のとおりです。

$.ajax私は私の要求でこれを変更しました:

// dataType: 'json', do not define the dataType let the jQuery infer the type !
data: form.serialize(),
success: function (result)
{
    debugger;
    if (result.success) {
        $dialog.dialog("close");
        // Update UI with Json data          
    }
    else {
        // Reload the dialog with the form to show model/validation errors 
        $dialog.html(result);
    }
} // success end

投稿アクションは次のようになります。

[HttpPost]
public ActionResult JsonCreate(Template template)
{
    if (!ModelState.IsValid) {
        return PartialView("Create", template);
        _templateDataProvider.AddTemplate(template);
        return Json(new { success = true });
    }
}

フォームであるpartialviewを返すか(success == false)、であるJSONを返しますsuccess == true

クライアント側のUIを更新するために、誰かがアイテムのリストを返す可能性があります。

return Json(new { success = true, items = list});
4

2 に答える 2

0

さて、私が見た間違いは-

あなたはコントローラーでリターンを行っています、あなたはすることになっています

Response.Write(Json(new { success = true, items = list}));
于 2012-05-27T16:28:03.583 に答える
0

html やその他の種類のデータではなく、JSON データを返すことを確認する必要があります。ブラウザー コンソールを使用して、Ajax の要求と応答について詳しく調べ、期待どおりに正しい応答が得られていることを確認してください。

于 2012-05-20T23:06:37.420 に答える