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

心已赠人 2022-10-07 07:58 135阅读 0赞

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 条评论,135人围观)

还没有评论,来说两句吧...

相关阅读

    相关 how to use kvm

    kvm 是qemu 集成了一些針對開啟了VT技術的cpu而開發的不錯的內核級程式. 下面是一些簡要的說明:- <table style="border:1px solid