Asp.net MVC 3 custom error message

Source: Internet
Author: User

I like to customize the error information, such as adding an incorrect small icon and creating an incorrect style. I found that the default @ Html. ValidationMessage cannot reach my goal.

Create Model:

    Public ClassLogin_Index
{
[DisplayName ("Logon account")]
[Required (ErrorMessage= "Logon account cannot be blank")]
Public StringUser_name {Set;Get;}

[DisplayName ("Logon Password")]
[Required (ErrorMessage= "The logon password cannot be blank.")]
Public StringUser_password {Set;Get;}
}

Create HTML:

@ Using (Html. BeginForm ())
{
<FieldsetClass= "Form">
@ Html. ValidationImage ()
<P>
@ Html. LabelFor (m => m. user_name)
@ Html. TextBoxFor (m => m. user_name)
</P>
<P>
@ Html. LabelFor (m => m. user_password)
@ Html. TextBoxFor (m => m. user_password)
</P>
<ButtonType= "Submit"Class= "Positive"Name= "Submit">
<ImgSrc= "/Content/images/key.png"Alt= "Announcement" />Login</Button>
<UlId= "Forgottenpassword">
<LiClass= "Boldtext">|</Li>
<Li> <AHref= "/Login/forgot">What should I do if I forget my password?</A> </Li>
</Ul>
</Fieldset>
}

Action operation:

        /// <Summary>
///User Logon Process
/// </Summary>
/// <Returns> </returns>
[HttpPost]
PublicActionResult Index (Login_Index model)
{
If(ModelState. IsValid)
ReturnRedirectToAction ("Index","Home");
Else
{
ReturnView ();
}
}

Extended HtmlHelper:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.