The Html.ValidationMessageFor() is a strongly typed extension method. It displays a validation message if an error exists for the specified field in the ModelStateDictionary object.
In the above example, the first parameter in ValidationMessageFor method is a lambda expression to specify a property for which we want to show the error message. The second parameter is for custom error message and the third parameter is for html attributes like css, style etc.
The ValidationMessageFor() method will only display an error if you have configured DataAnnotations attribute to the specifed property in the model class. The following example is a Student model class where the DataAnnotations attribute "Required" is applied to the StudentName property.
Now, when the user submits a form without entering the StudentName then ASP.NET MVC uses the data- attribute of Html5 for the validation and the default validation message will be injected when validation error occurs, as shown below.
Html with Validation message:
<spanclass="field-validation-error text-danger"data-valmsg-for="StudentName"data-valmsg-replace="true">The StudentName field is required.</span>
The error message will appear as the image shown below.
Output of ValidationMessageFor() method
Custom Error Message:
You can display your own error message instead of the default error message as above. You can provide a custom error message either in the DataAnnotations attribute or the ValidationMessageFor() method.
Use the ErrorMessage parameter of the DataAnnotation attributes to provide your own custom error message as shown below.
When user wants post data, we are using textarea for input. This data is stored into database and we are retrieving it using razor @Html.DisplayFor(). The Data for textarea is like this (inputed by user) Shall I compare thee to a summer's day? Thou art more lovely and more temperate: Rough winds do shake the darling buds of May, And summer's lease hath all too short a date: Sometime too hot the eye of heaven shines, And often is his gold complexion dimm'd; and when we retrieve it using this, <em>@Html.DisplayFor(model => item.inputvalue)</em> but it is displayed as unformated text like below, Shall I compare thee to a summer’s day? Thou art more lovely and more temperate:Rough winds do shake thedarling buds of May,And summer’s lease hath all too short a date: Sometime too hot the eye of heaven shines,And often is his gold complexion dimm’d; How can we format display text? Solution: @Html.Raw(Model.Inputvalue.Replace(Environment.NewLine, "<br...
To create a new layout view in Visual Studio, right click on shared folder -> select Add -> click on New Item.. In the Add New Item dialogue box, select MVC 5 Layout Page (Razor) and give the layout page name as "_myLayoutPage.cshtml" and click Add .
A predicate is also a delegate like Func and Action delegates. It represents a method that contains a set of criteria and checks whether the passed parameter meets those criteria or not. A predicate delegate methods must take one input parameter and it then returns a boolean value - true or false. The Predicate delegate is defined in the System namespace as shown below: Predicate signature: public delegate bool Predicate < in T>(T obj); Same as other delegate types, Predicate can also be used with any method, anonymous method or lambda expression.
Comments
Post a Comment