asp.net MVC 3客户端验证提交空的形式或不正确地验证 [英] asp.net mvc 3 client validation submitting empty form or not validating properly

查看:98
本文介绍了asp.net MVC 3客户端验证提交空的形式或不正确地验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来好像我的客户端验证是不是验证正确。

It appears as if my client validation is not validating properly.

在我的日志在屏幕上,当我设置了用户名和密码,提交表单被清除,出现必填字段验证消息和形式并不总是贴。为什么验证清除我的领域,并说他们是空的?

On my log on screen, when I set my username and password and submit the form is cleared and the validation message appears for required fields and the form is not always posted. why do validation clear my fields and say that they are empty?

有时也形式发布,但有空白字段,以便模型绑定的服务器上失败

also sometimes the form is posted but with blank fields so model binding fails on the server

更加古怪的是,我已经禁用客户端验证,仍然失败了我的生产服务器上

the even weirder part is that i have disabled client side validation and still it fails on my production server

一切都在开发精

更新:
我的行动已经分开,甚至不同的动作名称

Update : my action are already seperated, even different action names

[HttpGet]
        public ActionResult LogOn()
        {
            LogOnModel model = new LogOnModel() { UserName = "", Password = "" };
            return View(model);
            //return View();
        }

  [HttpPost]
  public ActionResult LogIn(LogOnModel model, FormCollection fcol/*, string returnUrl*/)
        {
            //Request.Form.Count
            StringBuilder sb = new StringBuilder();
            sb.Append("<br/>Form collection: ");
            if (Request.Form.Count > 0)
            {

                NameValueCollection form = Request.Form;
                sb.Append("<Form collection>");
                for (int i = 0; i < fcol.Count; i++)
                    sb.AppendFormat("{0}:{1},", fcol.AllKeys[i].ToString(), fcol[i].ToString());
                sb.Append("</Form collection>");
            }
            sb.Append("<br/>Form : ");
            if (Request.Form.Count > 0)
            {
                NameValueCollection form = Request.Form;
                sb.Append("<form>");
                for (int i = 0; i < form.Count; i++)
                    sb.AppendFormat("{0}:{1},", form.AllKeys[i].ToString(), form[i].ToString());
                sb.Append("</form>");
            }
            sb.Append("<br/>QueryString : ");
            if (Request.Form.Count > 0)
            {
                NameValueCollection form = Request.QueryString;
                sb.Append("<QueryString>");
                for (int i = 0; i < form.Count; i++)
                    sb.AppendFormat("{0}:{1},", form.AllKeys[i].ToString(), form[i].ToString());
                sb.Append("</QueryString>");
            }
            if (model != null)
            {
                sb.Append("<br/>Profile(ProfileModel m) : ");
                sb.AppendFormat("m.username = {0}, m.password = {1}", model.UserName, model.Password);
            }
            if (!ModelState.IsValid)
            {
                sb.Append("<br/>Model errors :");
                var errors = from key in ModelState
                             let errorList = ModelState[key.Key].Errors
                             where errorList.Any()
                             select new
                             {
                                 Item = key.Key,
                                 Value = key.Value,
                                 errorList
                             };

                foreach (var errorList in errors)
                {
                    sb.AppendFormat(@"<br/>MODEL ERROR: [{0}] value:'{1}' ", errorList.Item, errorList.Value);
                    foreach (var error in errorList.errorList)
                    {
                        sb.AppendFormat(" ERROR message: [{0}] exception : '{1}'", error.ErrorMessage, error.Exception);
                    }
                }
            }
            Response.Write(sb);
            //return new ContentResult();
            if (model != null)
                Log(new Exception(string.Format("model username : {0}, password : {1}, request[username] {2} , request[password] : {3}", model.UserName, model.Password, Request["UserName"], Request["Password"])));
            try
            {
                if (ModelState.IsValid)
                {
                    Log(new Exception(string.Format("ModelState {0}", ModelState.IsValid)));
                    Log(new Exception(string.Format("credentials {0},{1}", model.UserName, model.Password)));
                    if (MembershipService.ValidateUser(model.UserName, model.Password))
                    {
                        Log(new Exception(string.Format("MembershipService.ValidateUser {0},{1}", model.UserName, model.Password)));

                        FormsService.SignIn(model.UserName, false/* model.RememberMe*/);
                        Log(new Exception(string.Format("FormsService.SignIn {0},{1}", model.UserName, model.Password)));

                        //if (Url.IsLocalUrl(returnUrl))
                        //    return Redirect(returnUrl);
                        //else
                        return RedirectToAction("Index", "Home");
                    }
                    else
                        ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }
            catch (Exception ex)
            {
                Elmah.SqlErrorLog.GetDefault(System.Web.HttpContext.Current).Log(new Elmah.Error(ex, System.Web.HttpContext.Current));
            }
            // If we got this far, something failed, redisplay form
            return View("LogOn", model);
        }

和我是剃须刀页面有验证摘要

and yes my razor page has validation summary

@Html.ValidationSummary(false, @LocalDealsResources.Strings.LogOnUnsuccessful)
@using (Html.BeginForm("LogIn", "Account"))
{
    <div>
        <fieldset>
            <legend>Account Information</legend>
            <div class="editor-label">
                @Html.LabelFor(m => m.UserName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.UserName, new { style = " width:200px" })
                @Html.ValidationMessageFor(m => m.UserName)
            </div>
            <div class="editor-label">
                @Html.LabelFor(m => m.Password)
            </div>
            <div class="editor-field">
                @Html.PasswordFor(m => m.Password, new { style = " width:200px" })
                @Html.ValidationMessageFor(m => m.Password)
            </div>
            <p>
                <input type="submit" value="@LocalDealsResources.Strings.LogOn" />
            </p>
        </fieldset>
    </div>
}

给表格一试
http://dealze.com.sapin.arvixe.com/Account/Logon

推荐答案

我终于发现是什么原因导致这种

I finally found what was causing this

在我的Application_BeginRequest我登录在不同的线程的信息,我想继续申请该线程上运行,并且无法找到上下文请求

In my application_beginrequest I log the info on a different thread, I guess the application continued running on that thread and could not find the Request in the context

这篇关于asp.net MVC 3客户端验证提交空的形式或不正确地验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆