Code Highlighter

Sunday 18 May 2014

Basic ASP.NET Questions


What is the ViewBag object ?
ViewBag object is a member of Controller. It allows to pass the data from the controller to the view.

What is the functionality of the Html.ActionLink  ?
Html.ActionLink is an HTML helper method that comes from the MVC framework. Html.ActionLink renders the HTML links and it has 2 parameters :

  1. Test to display in the link
  2. Action to perform

 What is a strong strongly typed view ?
It's a view that intends to render a specific domain type.
Example :


The view.cshtlm containe the domain model as shown below

@model PartyInvites.Models.GuestResponse

@{
    Layout = null;
}

In ASP.NET MVC where shall we do the input validation ? 
It typically applied in the domain model, rather then in the user interface. ASP.NET MVC supports declarative validation rules defined with attributes from the System.ComponentModel.DataAnnotations.

Example: Validation the email input.

[Required(ErrorMessage = "Please enter your email address")]
[RegularExpression(".+\\@.+\\..+",
ErrorMessage = "Please enter a valid email address")]
public string Email { get; set; }

What is the functionality of the Html.ValidationSummary  ?
When there are errors, the Html.ValidationSummary allows to display the error messages defined by the validation attributes.
Example:


What is the MVC convention folder for the CSS style sheets ?
 By convention, the CSS style sheets shall place into the Content folder.

What Model-View-Controller Pattern ?
Models :  it can be view models, domain models, or operations for manipulation of those data.
View : it's the user interface that renders the models
Controllers:  which process the incoming requests, select views to render to the user, perform operations on the model.

The figure below is from the book Pro ASP.NET MVC and it's easy to remember.



What are the LINQ Extension Methods ?


Extension Method Deferred Description
All No Returns true if all the items in the source data match the predicate
Any No Returns true if at least one of the items in the source data matches the predicate
Contains No Returns true if the data source contains a specific item or value
Count No Returns the number of items in the data source
First No Returns the first item from the data source
FirstOrDefault No Returns the first item from the data source or the default value if there are no items
Last No Returns the last item in the data source
LastOrDefault No Returns the last item in the data source or the default value if there are no items
Max
Min
No Returns the largest or smallest value specified by a lambda expression
OrderBy
OrderByDescending
Yes Sorts the source data based on the value returned by the lambda expression
Reverse Yes Reverses the order of the items in the data source
Select Yes Projects a result from a query
SelectMany Yes Projects each data item into a sequence of items and then concatenates all of
those resulting sequences into a single sequence.
Single No Returns the first item from the data source or throws an exception if there are
SingleOrDefault No Returns the first item from the data source or the default value if there are no items, or throws an exception if there are multiple matches
Skip
SkipWhile
Yes Skips over a specified number of elements, or skips while the predicate matches
Sum No Totals the values selected by the predicate
Take
TakeWhile
Yes Selects a specified number of elements from the start of the data source or selects items while the predicate matches
ToArray
ToDictionary
ToList
No Converts the data source to an array or other collection type
Where Yes Filters items from the data source that do not match the predicate