[英]not displaying validation messages while click on button in mvc4
i have created a login page i want it to display validation messages if user doesnt enter username and password on click of button,i have written code as below 我已经创建了一个登录页面,如果用户在单击按钮时未输入用户名和密码,我希望它显示验证消息,我编写了如下代码
Homecontroller.cs Homecontroller.cs
public ActionResult Login()
{
// ViewBag.Message = "Your contact page.";
return View();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(logintable p)
{
abEntities db = new abEntities();
List<sp_logindata_Result> data = db.sp_logindata(p.Username, p.Password).ToList();
Session["LogedUserID"] = data.First().UserID.ToString();
Session["UserNme"] = data.First().FullName.ToString();
int typeVal = data.First().Usertype;
if (typeVal == 1)
{
return RedirectToAction("page1");
}
else if (typeVal == 2)
{
return RedirectToAction("page2");
}
else
{
return RedirectToAction("Error");
}
return View(data);
}
logintable.cs logintable.cs
public partial class logintable
{
public logintable()
{
this.PerformanceDetails = new HashSet<PerformanceDetail>();
this.EvaluationDetails = new HashSet<EvaluationDetail>();
}
public string FullName { get; set; }
[Required(ErrorMessage = "Employee name is required")]
public string Username { get; set; }
[Required(ErrorMessage = "Password is required")]
public string Password { get; set; }
public virtual ICollection<PerformanceDetail> PerformanceDetails { get; set; }
public virtual ICollection<EvaluationDetail> EvaluationDetails { get; set; }
}
login.cshtml login.cshtml
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div>
<fieldset>
<legend>Login</legend>
<div class="editor-field">
@Html.TextBoxFor(u => u.Username)
@Html.ValidationMessageFor(u => u.Username)
</div>
<div class="editor-label">
@Html.LabelFor(u => u.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(u => u.Password)
@Html.ValidationMessageFor(u => u.Password)
</div>
<input type="submit" value="Log In" />
</fieldset>
</div>
} }
what i am missing here so it is not displaying messages. 我在这里想念的是什么,所以它不显示消息。
That won't happen automatically 那不会自动发生
You need to follow at minimum certain steps: 您需要至少遵循某些步骤:
Add the scripts that will take care of client side validation (unobstrusive validation) [Required]. 添加将用于客户端验证(非侵入式验证)的脚本[必需]。
@section Scripts { @Scripts.Render("~/bundles/jqueryval") }
Add the bundle in BundleConfig.cs file, under App_Start folder 将捆绑软件添加到App_Start文件夹下的BundleConfig.cs文件中
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include( "~/Scripts/jquery.unobtrusive*", ~/Scripts/jquery.validate*"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.