@html.validationmessagefor加样式,c# - How to use html.ValidationMessageFor - Stack Overflow

I'm trying to get my view to give me the error message next to the text box if a user enters something invalid (like a string where it's expecting a number). Instead, I'm getting an ugly error page saying validation failed when the user presses submit.

Here is a portion of my view:

@model MembershipTest.ViewModels.AddDriverViewModel

@{

Layout = "~/Views/Shared/_Layout.cshtml";

ViewBag.Title = "Add Drivers";

}

@using (Html.BeginForm("AddDriver", "Driver"))

{

@Html.AntiForgeryToken()

@Html.ValidationSummary(true)

Customer Information

@Html.Label("First Name:")

@Html.TextBoxFor(m => m.Driver.F_Name)

@Html.ValidationMessageFor(m => m.Driver.F_Name)

@Html.Label("Gender:")

@Html.RadioButtonFor(m => m.isMaleChecked, "true") Male

@Html.RadioButtonFor(m => m.isMaleChecked, "false")Female

@Html.Label("Last Name:")

@Html.TextBoxFor(m => m.Driver.L_Name)

@Html.ValidationMessageFor(m => m.Driver.L_Name)

And here is the relevant portion of my model:

[Required]

[StringLength(30)]

public string F_Name { get; set; }

[Required]

[StringLength(30)]

public string L_Name { get; set; }

In the post method of my controller, I make sure to use

if (ModelState.IsValid)

If the user happened to enter something like 50 characters long in the first name text box, I want an error to be displayed using the Html.ValidationMessageFor() right when they tab out of that text box, so they can see it before they press submit. Am I missing some jquery to make this happen? Maybe some using statement I need to include?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值