Below is top 100 MVC interview question and answers with examples for freshers and intermediate developers.
What is model binding or model binder?
When we post data from view to controller then model binder is responsible for binding html element values to model object. Below is the
different ways of model binding.
eg.
- Primitive type eg. ActionResult Index(Int Id, string Name)
- Complex type eg. ActionResult Index(Employee obj)
- Form collection
- Bind atribute-include eg. ActionResult Edit([Bind(Include = "StudentId, StudentName")] Student std)
- Bind atribute-exclude eg. ActionResult Edit([Bind(Exclude = "Age")] Student std)
How to use multiple model objects in single view?
Below are different ways to bind two or more than two models object in single view.
- ViewData
- ViewBag
- TempData
- Session
- ViewModel - Create two classes inside one class
- Dynamic Model -- extendobject 4.0
- Tuples
- Render Action Method
Can we run ASP.NET MVC application without _ViewStart.cshtml?
Yes.
How can you add Razor View Engine in ASP.NET MVC?
protected void Application_Start() { ViewEngines.Engines.Add(new RazorViewEngine()); //Add Razor Engine here }
How to increase performance or optimize MVC application?
- Application Caching
- Optimize Your Images
- Use Sprites
- ETags
- Bundle/Minify JavaScript/CSS
- Compression/Zip
- Minified HTML
- Use AJAX When You Can
- Minimize Database Calls
- Use third-party services like Disqus for commenting system
What is Data Annotations in ASP.NET MVC?
Data Annotations is use to apply validation to web application by using Data Annotation attribute classes to model class.
Data Annotation attribute classes are present in System.ComponentModel.DataAnnotations
namespace.
Data Annotations help us to define the rules to the model classes or properties for data validation and displaying suitable messages to end users.
Different Annotation Validator Attributes are shown below.
- DataType: Specify the datatype of a property.
- DisplayName: Specify the display name for a property.
- DisplayFormat: Specify the display format for a property like different format for Date property.
- Required:Specify a property as required.
- ReqularExpression:Validate the value of a property by specified regular expression pattern.
- Range:Validate the value of a property within a specified range of values.
- StringLength:Specify min and max length for a string property.
- MaxLength:Specify max length for a string property.
- Bind:Specify fields to include or exclude when adding parameter or form values to model properties.
- ScaffoldColumn:Specify fields for hiding from editor forms.
How to read model object using filter in ASP.NET MVC?
We can access/read model object by creating custom filter as shown below.
Step:1 Creating custom filter.
public class MyTestAttribute : FilterAttribute, IActionFilter { public void OnActionExecuting(ActionExecutingContext filterContext) { Employee emp = null; foreach (object parameter in filterContext.ActionParameters.Values) { if (parameter is Employee) { emp = (Employee)parameter; break; } } if (emp != null) { string Id = emp.Id; string Name = emp.Name; } } public void OnActionExecuted(ActionExecutedContext filterContext) { // } }
Step:2 Applying custom filter on Action method.
[HttpPost] [MyTest] public ActionResult Index2(Employee obj) { return View(); }
How to read parameter value in ASP.NET MVC using custom action filter?
We can access/read parameter value creating custom filter as shown below.
Step:1 Creating custom filter.
public class MyTestAttribute : FilterAttribute, IActionFilter { public void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext.ActionParameters.ContainsKey("Name")) { string Name = filterContext.ActionParameters["Name"] as string?; } } public void OnActionExecuted(ActionExecutedContext filterContext) { // } }
Step:2 Applying custom filter on Action method.
[HttpPost] [MyTest] public ActionResult Index2(Employee obj, string Name) { return View(); }
What is default http verb for action method?
By default, action method support all the http verbs like HttpGet,HttpPost, HttpPut ..etc. If you explicitely specify the any of the verb, than you can not acces action method by using other http verbs. Ex. If you specify HttpGet verb on action method then, you can not access the action method using HttpPost,HttpPut,.. ..etc.
How to determine which http verb was used on Action method?
We can create custom filter to check http verb as shown below.
Step:1 Creating custom filter.
public class MyTestVerbAttribute : ActionMethodSelectorAttribute { public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo) { string verbType = controllerContext.HttpContext.Request.HttpMethod; return true; } }
Step:2 Applying custom filter on Action method. Apply on 2 different methods.
[HttpGet] public ActionResult Index2() { return View(); } [HttpPost] [MyTestVerbAttribute] public ActionResult Index2(Employee obj, string Name) { return View(); }