ASP.NET Mvc3 Jquery对话框验证摘要错误显示?

时间:2012-07-09 09:00:27

标签: asp.net-mvc jquery-ui validationsummary

对话框内容

<div id="div_dialog_container" class="dialog_container">
@using (Html.BeginForm((string)ViewBag.FormAction, "Musteri"))
{
    <div id="div_iu_form_container" class="ui_form_container">
        <div>@Html.ValidationSummary(true, "Müşteri Kaydı Başarısız! Lütfen Bilgileri Kontrol Ediniz.")
        </div>
        <table>
            <thead>
                <tr>
                    <th colspan="2">Genel Bilgiler</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>@Html.LabelFor(x => x.musteri_no):</td>
                    <td>@Html.TextBoxFor(x => x.musteri_no)
                        @Html.ValidationMessageFor(x => x.musteri_no)</td>
                </tr>
                <tr>
                    <td>@Html.LabelFor(x => x.musteri_adi):</td>
                    <td>@Html.TextBoxFor(x => x.musteri_adi)
                        @Html.ValidationMessageFor(x => x.musteri_adi)</td>
                </tr>
                <tr>
                    <td>@Html.LabelFor(x => x.sektor):</td>
                    <td>@Html.TextBoxFor(x => x.sektor)
                        @Html.ValidationMessageFor(x => x.sektor)</td>
                </tr>
            </tbody>
            <tfoot></tfoot>
        </table>

控制器

[HttpPost]
    public JsonResult JsonMusteriDuzenle(TblMusteriler musteri, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            try
            {
                mus_dbo.update_musteri(musteri);
                return Json(new { success = true, redirect = returnUrl });
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", "Müşteri güncelleme hatası.");
            }
        }

        return Json(new { errors = GetErrorsFromModelState() });
    }

我想显示文本框的错误。但它显示在这样的顶部:

enter image description here

如何显示每个文本框自身的错误?

我想要这个:

enter image description here

感谢。

2 个答案:

答案 0 :(得分:2)

在两种情况下,您都会从控制器操作返回JSON。您不能指望显示任何错误。如果要显示错误,则应返回包含表单的部分视图:

[HttpPost]
public ActionResult JsonMusteriDuzenle(TblMusteriler musteri, string returnUrl)
{
    if (ModelState.IsValid)
    {
        try
        {
            mus_dbo.update_musteri(musteri);
            return Json(new { success = true, redirect = returnUrl });
        }
        catch (Exception e)
        {
            ModelState.AddModelError("", "Müşteri güncelleme hatası.");
        }
    }

    return PartialView("_NameOfPartialContainingTheForm", musteri);
}

然后在调用此操作的javascript代码中,您必须将包含您的表单的div的内容替换为新的部分:

success: function(result) {
    if (result.success) {
        // the controller action return JSON success
        alert('Thanks');
    } else {
        // The controller action returned a PartialView
        // So now you have to refresh the DOM if you want 
        // to see errors showing up
        $('#id_of_some_div_that_contains_the_partial').html(result);
    }  
}

这假设您在主视图中有一个包含div:

<div id="id_of_some_div_that_contains_the_partial">
    @Html.Partial("_NameOfPartialContainingTheForm")
</div>

答案 1 :(得分:0)

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

这些行在我的母版页中,我在部分页面中添加了这些行。然后它按照我的预期工作。

感谢您的建议。