0

对话内容

<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() });
    }

我想显示文本框的错误按钮。但它显示在顶部,如下所示:

在此处输入图像描述

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

我要这个:

在此处输入图像描述

谢谢。

4

2 回答 2

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>
于 2012-07-09T09:44:29.397 回答
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>

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

感谢您的建议。

于 2012-07-09T11:23:20.237 回答