看起來好像我的客戶端驗證未正確驗證。asp.net mvc 3客戶端驗證提交空表單或未正確驗證
在我的登錄屏幕上,當我設置我的用戶名和密碼並且提交表單被清除並且驗證消息出現在必填字段中時,表單並不總是被髮布。爲什麼驗證要清理我的領域並說他們是空的?
有時也形式發佈,但有空白字段,以便模型綁定的服務器上失敗
更加古怪的是,我已經禁用客戶端驗證,仍然是我的生產服務器
一切上失敗在開發精
更新: 我的行動已經分隔,甚至不同的動作名稱
[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);
}
和是我的剃鬚刀頁面有驗證摘要
@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
您是否有機會到服務器進行往返訪問,提交數據並獲取新視圖,而無需模型,因此爲空白表單?根據我的經驗,驗證器將永遠不會「擦除」表單中的任何值,這只是它的工作原理。 –
現在你提到它它是有道理的,我猜它與我的其他帖子有關:http://stackoverflow.com/questions/8435583/asp-net-mv3-form-post-data-not-making-its-way-到的控制器動作。由於我的表單沒有發佈,因此視圖會重新加載空字段。我的模型是無效的,因爲所有字段都沒有從表單/模型綁定中填充,並且它使用空字段重新加載頁面 – freddoo