简体   繁体   English

如何使用javascript警报窗口显示验证消息?

[英]How to display validation messages with javascript alert window?

I use LogOn partial view on Index view and use default LogOnModel of MVC3. 我在Index视图上使用LogOn局部视图,并使用MVC3的默认LogOnModel。 But, there is not enough place to display validation messages in view. 但是,没有足够的地方在视图中显示验证消息。 How can I display validation messages with javascript alert window? 如何使用javascript警报窗口显示验证消息?

[HttpPost]
public ActionResult LogOn( LogOnModel model, string returnUrl )
{
    if ( ModelState.IsValid )
    {
        if ( ( model.UserName != null ) && ( model.Password != null ) )
        {
            if ( Membership.ValidateUser( model.UserName, model.Password ) )
            {
                FormsAuthentication.SetAuthCookie( model.UserName, true );

                if ( Url.IsLocalUrl( returnUrl ) && returnUrl.Length > 1 && returnUrl.StartsWith( "/" )
                    && !returnUrl.StartsWith( "//" ) && !returnUrl.StartsWith( "/\\" ) )
                {
                    return Redirect( returnUrl );
                }
                else
                {
                    return RedirectToAction( "Index", "Home", model );
                }
            }
            else
            {
                return RedirectToAction( "Index", "Home" );
            }
        }
        else return RedirectToAction( "Index", "Home" );
    }
    return RedirectToAction( "Index", "Home" );
}

In Index view: 在索引视图中:

 @Html.Partial( "LogOn" )   

And LogOn view: 和LogOn视图:

@model OnlineMarket.Models.LogOnModel
@using ( Html.BeginForm( "LogOn", "Home", FormMethod.Post ) )
{
    @Html.ValidationSummary( true )
    @Html.Hidden( "RememberMe", "true" )
    <ul class="forms">
        <li class="txt">Username</li>
        <li class="inputfield">
            @Html.TextBoxFor( m => m.UserName, new { @class = "logininputs" } )
        </li>
    </ul>
    <ul class="forms">
        <li class="txt" style="width: 26px;">Password</li>
        <li class="inputfield">
            @Html.TextBoxFor( m => m.Password, new { @class = "logininputs" } )
        </li>
    </ul>
    <ul class="forms">
        <li class="txt" style="width: 50px; margin-top: -5px; margin-left: 4px;">
            <input type="submit" value="Enter" class="buttonforinput" style="height: 23px;" />
        </li>
    </ul>
}

For the unobtrusive client validation you could subscribe to the showErrors callback and display the errors the way you want ( alert in your case): 对于不显眼的客户端验证,您可以订阅showErrors回调并以您希望的方式显示错误(在您的情况下alert ):

(function ($) {
    $.validator.setDefaults({
        onsubmit: true,
        onkeyup: false,
        onfocusout: false,
        showErrors: function (errorMap, errorList) {
            if (errorList.length > 0) {
                var errors = errorList.map(function (element) {
                    return element.message;
                }).join('\r\n');
                alert(errors);
            }
        }
    });
})(jQuery);

In order to display server side errors with alert on the client you shouldn't be using the @Html.ValidationSummary( true ) helper because this helper outputs the errors directly in the view and you said that you don't have enough place. 为了在客户端上显示带有alert服务器端错误,您不应该使用@Html.ValidationSummary( true )帮助程序,因为此帮助程序直接在视图中输出错误,并且您说您没有足够的位置。

So you could emit javascript dynamically when there are errors and display them in an alert : 因此,您可以在出现错误时动态发出javascript并在alert显示它们:

@if (!ViewData.ModelState.IsValid)
{
    <script type="text/javascript">
        var errors = @Html.Raw(
            Json.Encode(
                string.Join(
                    Environment.NewLine, 
                    ViewData.ModelState
                            .Where(x => x.Value.Errors.Count > 0)
                            .SelectMany(x => x.Value.Errors)
                            .Select(error => error.ErrorMessage))
                )
        );
        alert(errors);
    </script>
}

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

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