2 Explain the concept of MVC Scaffolding?
Answer
ASP.NET Scaffolding is a code generation framework for ASP.NET Web applications. Visual Studio 2013 includes pre-installed code generators for MVC and Web API projects. You add scaffolding to your project when you want to quickly add code that interacts with data models. Using scaffolding can reduce the amount of time to develop standard data operations in your project.
Scaffolding consists of page templates, entity page templates, field page templates, and filter templates. These templates are called Scaffold templates and allow you to quickly build a functional data-driven Website.
Scaffolding Templates
Create
It creates a View that helps in creating a new record for the Model. It automatically generates a label and input field for each property in the Model.
Delete
It creates a list of records from the model collection along with the delete link with delete record.
Details
It generates a view that displays the label and an input field of the each property of the Model in the MVC framework.
Edit
It creates a View with a form that helps in editing the current Model. It also generates a form with label and field for each property of the model.
List
It generally creates a View with the help of a HTML table that lists the Models from the Model Collection. It also generates a HTML table column for each property of the Model.
Learn more here – Terminologies in MVC: Part 3 (Scaffolding)
1 Explain what is the difference between View and Partial View?
View
Partial View
Learn more here – Partial View in MVC
Server Side Validation in MVC?
Answer
The ASP.NET MVC Framework validates any data passed to the controller action that is executing, It populates a ModelState object with any validation failures that it finds and passes that object to the controller. Then the controller actions can query the ModelState to discover whether the request is valid and react accordingly.
I will use two approaches in this article to validate a model data. One is to manually add an error to the ModelState object and another uses the Data Annotation API to validate the model data.
Approach 1 – Manually Add Error to ModelState object
I create a User class under the Models folder. The User class has two properties “Name” and “Email”. The “Name” field has required field validations while the “Email” field has Email validation. So lets see the procedure to implement the validation. Create the User Model as in the following,
After that I create a controller action in User Controller (UserController.cs under Controllers folder). That action method has logic for the required validation for Name and Email validation on the Email field. I add an error message on ModelState with a key and that message will be shown on the view whenever the data is not to be validated in the model.
Thereafter I create a view (Index.cshtml) for the user input under the User folder.