Unit-3 (Model Validation and Annotation)
Unit-3 (Model Validation and Annotation)
Unit-3 (Model Validation and Annotation)
• The model class can be used in the view to populate the data, as well
as sending data to the controller.
• If your application does not have data, then there is no need for a model. If
your application has data, then you need a model.
• in simple words, we can say that the Model is the component in the MVC
Design pattern that is used to manage the data, i.e., the state of the
application in memory.
• in that MVC Application, three things are common, i.e., Model, View, and
Controller.
• The Controllers are used to manage the overall flow of the MVC
Application.
• Models are responsible for storing the data that is used on Views.
• Views are basically the HTML Pages that get rendered into the client’s
browser. In the browser, we generally perform two operations.
• First, we display the data to the user; second, we get the data from the
user. For both these operations, models are used.
Query String and Route Parameter Binding using Model:
Model Binding using Model Class:
• Model binding is a process in which we bind a model to controller and
view.
• It is a simple way to map posted form values to a .NET Framework
type and pass the type to an action method as a parameter.
• It acts as a converter because it can convert HTTP requests into
objects that are passed to an action method.
• Here data are stored in seprate model class and this model class
sending data to a specific action method.
• Model binding are done by three ways,
1. QueryString Parameter
2. Passing Route Parameter
3. By creating Model class.
Controller View
(send data for process (Receive data From
and result) Model)
Example:
Model Validation:-
• Validation is an important aspect in ASP.NET MVC applications.
• It is used to check whether the user input is valid. ASP.NET MVC
provides a set of validation that is easy-to-use and at the same time.
• it is also a powerful way to check for errors and, if necessary, display
messages to the user.
• These validations ensure that the user fills in the correct data into the
application so that we can process that data and save it for future
use.
• Normally, users always fill out the form with the necessary
information and submit it.
• we notice that the site gives us some error message or warning
message.
• when we do not put properly formatted data in the form's controls.
Normally, we receive the message as below –
1. The field value is required. We can’t leave the field blank.
2. Please enter your data in the correct format in case of phone number, date
of birth, etc.
3. Please provide a valid email address.
4. Your password does not match the format like uppercase, one symbol, and
one number.
• This type of message is known as form validation.
Validation Types :-
Validations
• Step-3:
"ConnectionStrings": {
"dbcs3":
"Server=127.0.0.1;port=3307;Database=Stud;user=root;password=;“}
• Step-4:
builder.Services.AddDbContext<StudentDBContext>(item =>
item.UseMySql(builder.Configuration.GetConnectionString("dbcs3"),
ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("dbcs3"))
));
• Step-5:
DbContext File:
2]: DatabaseFirst Connectivity:
Steps for DatabaseFirst Connectivity:
• Step-1:
• Step-2:
• Note: When example are required so please define example in
specific points