Wednesday, November 23, 2011

What is difference between ViewData,ViewBag and TempData in MVC 3.0

ViewData: ViewData is a dictionary of objects that are accessible using strings as keys Eg.
1.ViewData[“JustLearn”]=”Just Learn on Beyond relational is rock!!”
ViewBag:ViewBag uses the dynamic feature that was added in to C# 4.0 It allows an object to dynamically have properties added to it. We can say viewBag=ViewData + dynamic wrapper around the ViewData dictionary. Eg.
1.ViewBag.JustLearn =”Just Learn on beyondrelational is rock!!”
TempData: Temp data use during the current and subsequent request only means it is use when you are sure that next request will be redirecting to next view. Eg.
view source
print?
1.TempData[“JustLearn”]=”Just Learn on beyondrelational is rock!!”
Hold Up

Search and Filters in Error List Window – Visual Studio 2011 Developer Preview

The Visual Studio “Error List Window” helps you display the errors, warnings, and messages produced during compiling code, build, code analysis or even some other scenarios as well. We can click on the particular error, warning or message to get the exact details and cursor moves to location where error or warning causes. When we are dealing with multiple project in a single solution, it is very much difficult to find out exact error within a specific file or with in a solution. We need to scroll through entire error list to get the file, errors, messages or warnings. Visual Studio 2011 introduced search and filtering with in error list window.

Visual Studio 2011 added a search box to the Error List to help you find the error, warning, or message. Just for example, if you are looking for an error with in a specific file, just enter the file name in search box, Error list will display the error, warnings and messages from the given file.

New Color Picker in Visual Studio 2011 Developer Preview for CSS

CSS editor in Visual Studio 2011 introduced many new features for web developers and it helps developers to work with CSS more efficiently and organize way. One of the nice features of the new CSS Editor is Color Picker. While designing styles for web applications, web developers refers different tool to choose the color and color code. But now, there is an integrated Color Picker that helps developer to pick up the color within editor itself.

In CSS Editor, locate the cursor next to any color attribute and press Space. A color pickup automatically popups up. You can select the color as per your requirements.

Hold Up

Creating Custom HTML Helpers in ASP.NET MVC

List of built-in HTML Helpers provided by ASP.NET MVC.

ActionLink() – Links to an action method.
BeginForm() – Marks the start of a form and links to the action method that renders the form.
CheckBox() – Renders a check box.
DropDownList() – Renders a drop-down list.
Hidden() – Embeds information in the form that is not rendered for the user to see.
ListBox() – Renders a list box.
Password() – Renders a text box for entering a password.
RadioButton() – Renders a radio button.TextArea() – Renders a text area (multi-line text box).
TextBox () – Renders a text box.

How to develop our own Custom HTML Helpers?

For developing custom HTML helpers the simplest way is to write an extension method for the HtmlHelper class. See the below code, it builds a custom Image HTML Helper for generating image tag.

using System;
using System.Web.Mvc;
namespace MvcHelpers
{
public static class CustomHtmlHelpers
{
public static TagBuilder Image(this HtmlHelper helper, string imageUrl, string alt)
{
if (!String.IsNullOrEmpty(imageUrl))
{
TagBuilder imageTag = new TagBuilder("img");
imageTag.MergeAttribute("src", imageUrl);
imageTag.MergeAttribute("alt", alt);
return imageTag;
}
return null;
}
}
}

TagBuilder represents a class that is used by HTML helpers to build HTML elements.
TagBuilder.MergeAttribute(String, String) method adds an attribute to the tag by using the specified key/value pair.

The next step is in order to make it available to Views, we should include the name space of out custom HTML helper class into Web.config

Build the solution to make sure the helper method is available to views. Goto any view in the solution access the newly created HTML image helper method.

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>


Home




<%= Html.Encode(ViewData["Message"]) %>


<%=Html.Image("/Images/logo.png","logo") %>





<%=Html.Image(“/Images/logo.png”,”logo”) %>

The image HTML helper generates the following HTML.

”logo”/

Image Tag Helper is simple example of HTML helpers, we can put any sort of complex logic into HTML Helpers & access them with in the views with a simple method call. in turn which makes the view simpler.

How To Create Session-less Controller in MVC3

How to manage the controller’s session state?

Simply we can decorate the controller class with “SessionState” attribute. [SessionState()] attribute accepts SessionStateBehaviour enumeration.

SessionStateBehaviour enumeration has the following constants.

SessionStateBehavior.Default - ASP.NET default logic is used to determine the session state behavior for the request.
SessionStateBehavior.Required – Full read-write session state behavior is enabled for the request.
SessionStateBehavior.ReadOnly – Read only session state is enabled for the request.
SessionStateBehavior.Disabled – Session state is not enabled for processing the request.

How to return 404 Http status code from ASP.NET MVC Application?

ASP.NET MVC3 includes a new class HttpNotFoundResult in System.Web.Mvc namespace.

HttpNotFoundResult: Instance of HttpNotFoundResult class indicates to client(browser) that the requested resource was not found. It returns a 404 HTTP status code to the client. Generally we return 404 status code if a requested webpage is not available. In case of MVC applications we return 404 status code is in terms of resources, for example we are searching for particular user profile in the portal, if the user profile is not found, we can return 404.

How to return 404 status code from a MVC application?

First way is to instantiate HttpNotFoundResult class and return the object.

public ActionResult Index()
{
var result = new HttpNotFoundResult();
return result;
}

Next alternative is to makes use of HttpNotFound() helper method of the Controller class which returns the HttpNotFoundResult instance.

public ActionResult Index()
{
return HttpNotFound();
}

we can return 404 along with custom status code description,using the overloded version of HttpNotFound(string statusDescription).

public ActionResult Index()
{
return HttpNotFound("can not find requested resource");
}

How to select Block of Code in Visual Studio?

You can select line by line code very easily using mouse or keyboard. But if you want to copy some code block where you don’t to select all part of line or something like that, here is a Quick TIP For you. Press and Hold “Alt” Key, and select the code region..