简体   繁体   English

ValidationSummary 不显示错误

[英]ValidationSummary doesn't display errors

I have a page that charges a credit card.我有一个向信用卡收费的页面。 When I attempt to charge the card, I'd like to redisplay the page if I get an error back as a response.当我尝试从卡中扣款时,如果我收到错误作为响应,我想重新显示该页面。

Here's my controller method:这是我的 controller 方法:

[HttpPost]
public ActionResult Charge(CreditCardViewModel viewModel)
{
    if (ModelState.IsValid)
    {
        var request = new AuthorizationRequest(viewModel.CreditCardNumber,
            viewModel.ExpirationDate.Value.ToString("MMyy"),
            viewModel.Amount.Value, "");
        var gate = new Gateway("XXXXXXXXX", "XXXXXXXXX", true);
        var response = gate.Send(request);

        if (!response.Approved)
        {
            ModelState.AddModelError("", response.Message);
            return View(viewModel);
        }
        else
        {
            viewModel.ResponseMessage = response.Message;
            return View("Results", viewModel);
        }
    }
    return View(viewModel);     // validation error, so redisplay same view
}

And my view:而我的观点:

<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm("Charge", "CreditCard", FormMethod.Post)) { %>

    <div class="editor-label"><%: Html.LabelFor(m => m.CreditCardNumber) %></div>
    <div class="editor-field">
        <%: Html.TextBoxFor(m => m.CreditCardNumber)%>
        <%: Html.ValidationMessageFor(m => m.CreditCardNumber)%>
    </div>

    <div class="editor-label"><%: Html.LabelFor(m => m.ExpirationDate) %></div>
    <div class="editor-field">
        <%: Html.TextBoxFor(m => m.ExpirationDate)%>
        <%: Html.ValidationMessageFor(m => m.ExpirationDate)%>
    </div>

    <div class="editor-label"><%: Html.LabelFor(m => m.Amount) %></div>
    <div class="editor-field">
        <%: Html.TextBoxFor(m => m.Amount)%>
        <%: Html.ValidationMessageFor(m => m.Amount)%>
    </div>

    <div class="buttons">
        <input type="submit" value="Charge Amount" />
    </div> 

    <% Html.ValidationSummary(false); %>

<% } %>

The code works correctly - if I get an error back as a response, the view is reloaded.代码正常工作 - 如果我收到错误作为响应,则重新加载视图。 The only problem is that no error is displayed by the validation summary.唯一的问题是验证摘要没有显示任何错误。

One strange thing is that if I change the AddModelError line to:一件奇怪的事情是,如果我将AddModelError行更改为:

ModelState.AddModelError("CreditCardNumber", response.Message);

It will show the error next to the CreditCardNumber textbox.它将在 CreditCardNumber 文本框旁边显示错误。 But I'd like to display the error in the summary below the form, as sometimes the error may not be with the credit card.但我想在表格下方的摘要中显示错误,因为有时错误可能与信用卡无关。

Try this (pay attention to colon) as it returns MvcHtmlstring:试试这个(注意冒号),因为它返回 MvcHtmlstring:

<%: Html.ValidationSummary(false) %>

It is ok to give empty string, it will be treated as not field error.可以给出空字符串,它将被视为非字段错误。

Add another ValidationMessage output.添加另一个 ValidationMessage output。

<%= Html.ValidationMessage("GatewayError") %>

And set the error message accordingly并相应地设置错误消息

ModelState.AddModelError("GatewayError", response.Message);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM