Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 在MVC中获取服务器端错误的实例上,无法在模式中显示部分视图_Asp.net Mvc_Asp.net Mvc Partialview_Server Side Validation - Fatal编程技术网

Asp.net mvc 在MVC中获取服务器端错误的实例上,无法在模式中显示部分视图

Asp.net mvc 在MVC中获取服务器端错误的实例上,无法在模式中显示部分视图,asp.net-mvc,asp.net-mvc-partialview,server-side-validation,Asp.net Mvc,Asp.net Mvc Partialview,Server Side Validation,从过去的三天开始,我在这方面一直很努力。我有一个_布局页面,在该页面中,单击Login按钮,我会在一个模式div中加载一个Login部分视图。当我提交时,如果有任何客户端验证可以正常工作,表单在给出正确数据之前不会提交发生服务器端验证时出现问题。如果凭据无效,它将重定向到同一页面,但不会显示模式。当我再次单击登录按钮时,无效凭证错误消息将显示在模式中。我想知道,通过我正在尝试的方式,这是否是可能的,或者是否有其他方法来实现这一点 这是布局页面的一部分: <a class="btn btn-

从过去的三天开始,我在这方面一直很努力。我有一个_布局页面,在该页面中,单击Login按钮,我会在一个模式div中加载一个Login部分视图。当我提交时,如果有任何客户端验证可以正常工作,表单在给出正确数据之前不会提交发生服务器端验证时出现问题。如果凭据无效,它将重定向到同一页面,但不会显示模式。当我再次单击登录按钮时,无效凭证错误消息将显示在模式中。我想知道,通过我正在尝试的方式,这是否是可能的,或者是否有其他方法来实现这一点

这是布局页面的一部分:

<a class="btn btn-style btn-success btn-lg btn-wid" data-target="#modal" data-toggle="modal" id="flogin"> Login </a>
@model Sample.Models.LoginModel
@section Validation {
    @Scripts.Render("~/bundles/jqueryval")
}

<section id="Login">

    @using (Html.BeginForm("Login", "Home", FormMethod.Post, new { enctype = "multipart/form-data", id = "LoginForm", ReturnUrl = ViewBag.ReturnUrl }))
    { 
        <div class="modal-content wrap col-md-4 col-xs-12 col-sm-12 col-md-offset-4">

            @Html.AntiForgeryToken()
            <div class="row">
                <button type="button" class="close" id="btnLoginModalClose" style="margin-top:-15px" data-dismiss="modal" aria-hidden="true">×</button>
            </div>

            <div class="row-fluid">
                <div id="LoginDialog" class="col-md-12">
                    <div class="modal-header">
                        <h3 id="login" class="modal-title">
                            Freshers Login
                        </h3>
                        <div class="ui-state-error-text">
                            @if (TempData["ModelState"] != null)
                            {
                                @TempData["ModelState"]
                            }

                        </div>
                    </div>

                    <div class="modal-body">
                        <div class="form-group">
                            <span class="ico-email"></span>
                            @Html.TextBoxFor(m => m.UserName, new { id = "floginEmail", placeholder = "Email", type = "email", @class = "input-lg ipt ip-email" })
                            <div class="text-danger">
                                @Html.ValidationMessageFor(model => model.UserName)
                            </div>
                        </div>
                        <div class="form-group">
                            <span class="ico-pwd"></span>
                            @Html.TextBoxFor(m => m.Password, new { id = "floginPassword", placeholder = "Password", type = "password", @class = "input-lg ipt ip-pwd" })

                            <a href="#" id="loginpswdshow" onclick="showhide($(this));"><span style="position:relative" id="sploginpswdshow" class="glyphicon glyphicon-eye-open show"></span></a>
                            <div class="text-danger">
                                @Html.ValidationMessageFor(model => model.Password)
                             </div>
                            </div>
                        <div class="form-group">

                            <div class="input-group">
                                <span class="input-group-addon">
                                    @Html.CheckBoxFor(m => m.RememberMe, new { id = "chkRemember", type = "checkbox", @class = "checkbox" })
                                </span>
                                @Html.LabelFor(m => m.RememberMe, new { @class = "form-control" })
                            </div><!-- /input-group -->
                        </div>
                    </div>
                    <div class="modal-footer" style="margin-top:14px">
                        <input type="submit" id="btnLogin" name="login" style="margin-top:-5px" class="btn btn-primary pull-left btn-flogin" value="Log-in" />
                        <div class="row-fluid">
                            <a class="text-center" style="font-size:medium" onclick="fresherForgotClick();" id="lnkForgot" href="#">Forgot password?</a>
                        </div>
                    </div>
                    <span class="text-info"><strong>Do not have an Account? <a class="btn alert-danger" id="btnCreateAccount">Click here to Create one!</a></strong></span>

                </div>
            </div>
        </div>
    }


</section>

请让我知道是否可以通过以下方式实现此目的。您的工作流程可能应该遵循以下方式:

  • 使用jQuery或类似工具异步发布表单,而不是正常发布表单
  • 返回带有真/假成功值和可选消息的JsonResult
  • 如果success值为true,则刷新页面,否则,将错误消息插入模式
  • 首先,将登录操作修改为类似以下内容:

    public ActionResult Login(Models.FreshersModel model, string returnUrl)
    {
        bool success = false;
        string message = "User name or password is incorrect.";
    
        if (ModelState.IsValid)
        {
            success = model.IsValid(model.UserName,model.Password);
            if(success)
            {
                message = string.Empty;
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
            }
        }
    
        return Json(new { success = success, message = message });
    }
    
    第二,我已经将您的大部分模态调整回(几乎)使模态插件工作所需的最小值:

    <div id="LoginDialog" class="modal hide">
        <div class="modal-header">
            <h3 id="login" class="modal-title">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                Freshers Login
            </h3>
        </div>
        <div class="modal-body">
            @using (Html.BeginForm("Login", "Home", new { returnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { id = "LoginForm", role="form" }))
            { 
                @Html.AntiForgeryToken()
    
                <div id="errors" class="ui-state-error-text">
                </div>
    
                <div class="form-group">
                    <span class="ico-email"></span>
                    @Html.TextBoxFor(m => m.UserName, new { id = "floginEmail", placeholder = "Email", type = "email", @class = "input-lg ipt ip-email" })
                    <div class="text-danger">
                        @Html.ValidationMessageFor(model => model.UserName)
                    </div>
                </div>
                <div class="form-group">
                    <span class="ico-pwd"></span>
                    @Html.PasswordFor(m => m.Password, new { id = "floginPassword", placeholder = "Password", @class = "input-lg ipt ip-pwd" })
                    <div class="text-danger">
                        @Html.ValidationMessageFor(model => model.Password)
                     </div>
                    </div>
                <div class="form-group">
                    <div class="input-group">
                        <span class="input-group-addon">
                            @Html.CheckBoxFor(m => m.RememberMe, new { id = "chkRemember", @class = "checkbox" })
                        </span>
                        @Html.LabelFor(m => m.RememberMe, new { @class = "form-control" })
                    </div><!-- /input-group -->
                </div>
            }
        </div>
        <div class="modal-footer">
            <a href="#" class="btn">Close</a>
            <button type="button" class="btn btn-primary" id="btnLogin">Login</button>
        </div>
    </div>
    

    我省略了一些部分,比如启动模式,但这应该可以让您开始了。

    所以您是说,我需要使用JsonResult类型,而不是ActionResult。如果您将流程修改为使用ajax,则可以。您可以将返回类型保留为ActionResult,因为JsonResult隐式转换。您建议使用哪种类型。。。?我的意思是,它是安全的,并且不会降低性能。不管怎样,它都不安全。性能是相对的。我将更新答案以显示我的建议。
    public ActionResult Login(Models.FreshersModel model, string returnUrl)
    {
        bool success = false;
        string message = "User name or password is incorrect.";
    
        if (ModelState.IsValid)
        {
            success = model.IsValid(model.UserName,model.Password);
            if(success)
            {
                message = string.Empty;
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
            }
        }
    
        return Json(new { success = success, message = message });
    }
    
    <div id="LoginDialog" class="modal hide">
        <div class="modal-header">
            <h3 id="login" class="modal-title">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                Freshers Login
            </h3>
        </div>
        <div class="modal-body">
            @using (Html.BeginForm("Login", "Home", new { returnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { id = "LoginForm", role="form" }))
            { 
                @Html.AntiForgeryToken()
    
                <div id="errors" class="ui-state-error-text">
                </div>
    
                <div class="form-group">
                    <span class="ico-email"></span>
                    @Html.TextBoxFor(m => m.UserName, new { id = "floginEmail", placeholder = "Email", type = "email", @class = "input-lg ipt ip-email" })
                    <div class="text-danger">
                        @Html.ValidationMessageFor(model => model.UserName)
                    </div>
                </div>
                <div class="form-group">
                    <span class="ico-pwd"></span>
                    @Html.PasswordFor(m => m.Password, new { id = "floginPassword", placeholder = "Password", @class = "input-lg ipt ip-pwd" })
                    <div class="text-danger">
                        @Html.ValidationMessageFor(model => model.Password)
                     </div>
                    </div>
                <div class="form-group">
                    <div class="input-group">
                        <span class="input-group-addon">
                            @Html.CheckBoxFor(m => m.RememberMe, new { id = "chkRemember", @class = "checkbox" })
                        </span>
                        @Html.LabelFor(m => m.RememberMe, new { @class = "form-control" })
                    </div><!-- /input-group -->
                </div>
            }
        </div>
        <div class="modal-footer">
            <a href="#" class="btn">Close</a>
            <button type="button" class="btn btn-primary" id="btnLogin">Login</button>
        </div>
    </div>
    
    $(function(){
    
        $(document)
            .on('submit', '#LoginForm', function(e) {   
                e.preventDefault(); /* we're taking over the default behaviour */
    
                var url = $(this).attr('action');
                var formData = $(this).serialize();
    
                $.post(url, formData)
                    .fail(function(jqxhr, status, error){
                        $('#errors').html('An error occurred. Please try again.');
                    })
                    .done(function(response, status, jqxhr){
                        if(response.success) {
                            window.location.redirect('/');
                        }
                        else {
                            $('#errors').html(response.message);
                        }
                    });
            })
            .on('click', '#btnSubmit', function(e) {
                e.preventDefault();
                $('#LoginForm').trigger('submit');
            });
    
    });