Showing posts with label .net. Show all posts
Showing posts with label .net. Show all posts

Sunday, January 22

ViewData, ViewBag and TempData in ASP.Net MVC

ViewBag

– ViewBag is used to send a small amount of data to the view.
– There may be some situation where you want to transfer some temporary data to view which is not included in the model object.
– The viewBag is a dynamic type property of ControllerBase class.
– It does not retain value when redirection occurs,it is available only for Current Request.
– ViewBag is a dynamic property and it makes use of the C# 4.0 dynamic features.
public class DemoController : Controller
{
IList CarList = new List() {
new Car(){ CarName=”i10″, CarColor=”Black”,CarMileage=18.2 },
new Car(){ CarName=”EON”, CarColor=”Silver”, CarMileage=18.2 },
new Car(){ CarName=”Swift”, CarColor=”Red”, CarMileage=18.2},
new Car(){ CarName=”Verna”, CarColor=”Black”, CarMileage=18.2},
new Car(){ CarName=”Ciaz”, CarColor=”Silver”, CarMileage=18.2}
};
public ActionResult Index()
{
ViewBag.TotalCar = CarList.Count();
return View();
}
}

ViewData

– ViewData is similar to ViewBag, It can be used to send data from controller to view.
– It is a dictionary , which possese key-value pairs, where each key must be string.
– Viewdata last long only for current http request.
public class DemoController : Controller
{
public ActionResult Index()
{
ViewData[“Simple”] = “This string is stored in viewData.”;
return View();
}
}
To get back value from viewData write below line in view.

@ViewData[“Simple”]

TempData

– TempData retain value across subsequent HTTP Request
– It can be use to maintain data between controller actions as well as redirects.
– It internally stores data in session but it get destroyed earliar than session.
In the below example, a string value is stored in the TempData object in JupiterController
and it is redirected to EarthController and finally it is displayed in View.
public class JupiterController : Controller
{
// GET: First
public ActionResult Index()
{
TempData[“Message”] = “Jupiter is largest planet in solar system”;
return new RedirectResult(@”~\Earth\”);
}
}
public class EarthController : Controller
{
// GET: Second
public ActionResult Index()
{
return View();
}
}
View of EarthController
<html>
<head>
<meta name=”viewport” content=”width=device-width”/>
<title>Solar System</title>
</head>
<body>
<div>
@TempData[“Message”];
</div>
</body>
</html>

Wednesday, January 11

Visual Studio 2013 Tips and Tricks

You can use below tips and trick in visual studio to enhance the speed of coding and development.
(1) Matching brace/comment/region/quote
“Ctrl+]” can be used to the match brace, region, quote. It can also be used to the match comment, region or quote depending on where is the cursor now.
(2) Create Property By Shortcut
Creating Properties are very common to get and/or set values. For writing Property, you do not need to write it completely. just type PROP and then press TAB key twice.
(3) Vertical block selection.
Select Alt key and then select the area you want with your mouse.

(4) Auto Complete
Using IntelliSense in Visual Studio you can complete method, variable, class, object, property etc. by just writing its initial. Use Ctrl + Space or Alt + Right Arrow.
(5) Bookmark
Using Bookmark you can navigate to code in visual studio.
Create/Remove Bookmark – Ctrl+K, Ctrl+K
Move to next bookmark – Ctrl+K, Ctrl+N
Move to previous bookmark – Ctrl+K, Ctrl+P
Clear all bookmarks – Ctrl+K, Ctrl+L

(6) Build and Debug

Build – Ctrl+Shift+B
Run – Ctrl+F5
Debug – F5
Cycle through build errors – F8

(7) Move Code Up or Down
If you want to move a line up then just keep the cursor on that line and press “Alt + Up Arrow key” similar to this if you want to move the line down then just keep the cursor on that line and press “Alt + Down Arrow key”.

(8) Comment Code block
Comments are used for documentation purpose or temporarily disable the code. You can comment block of code by selecting that portion and then press Ctrl-K + C. To uncomment code select code block and press Ctrl-K + U.

(9) Switch between Currently open tabs.
You can open lastly visited tab in visual studio by pressing Ctrl + Tab and for opposite direction Ctrl + Shift + Tab
So, these are some commonly used shortcuts which will save your time.

OOPS Concepts, Features & Explanation with Example in C#.Net

– OOPS = Object Oriented Programming.
– OOPS is a programming technique which provides an efficient way to manage Object and its behaviour across a system.
– OOPS provides a way to show/hide relevant data.
– OOPS provides an efficient way for code reusability.

Here I will explain you important concepts of OOPs.

(1) Class

– The class can be considered as a blueprint for an object.
– A class is a collection of object.
– It is compulsory to create a class for representation of data.
– Class do not occupy memory space, so it is a merely logical representation of data.

The syntax for declaring a class.

public class College
{
//your code goes here.
}

(2) Object

– The object is variable of type Class.
– Object possess property and behaviour.
– As a class do not occupy any memory, so to work with real-time data representation you need to make an object of the class.

Syntax
– Here is the syntax to create an object(we just created above).

College objCollege = new College();

(3) Encapsulation

– Encapsulation is the process of keeping item into one logical unit.
– Encapsulation is a technique used to protect the information in an object from another object.
– The concept of data hiding or information hiding can be achieved by encapsulation.
– In c#,Encapsulation is implemented using the access modifier keywords. C# provides 5 types of access modifier,
which are listed below.
(i) Public : Public members are accessible by any other code in the same  assembly or another assembly that reference it.
(ii)Private : Private member can only be accessed by code in the same class.
(iii)Protected: Protected member can only be accessed by code in the same class or in a derived class.
(iv)Internal : Internal member can be accessed by any code in the same assembly, but not from another assembly.
(v) Protected Internal : Protected Internal member can be accessed by any code in the same assembly, or by any derived class in another assembly.

(4) Polymorphism

– Polymorphism is an ability to take more than one form in different case/scenario.
– It can make different logical units but with the same name. Its behaviour is executed based on input or the way it is called.

class PolymorphismExample
{
public void Add(int p_Value1, int p_Value2)
{
Console.WriteLine(“Result ={0}”, p_Value1 + p_Value2); // This will perform addition of p_Value1 and p_Value2
}

public void Add(string p_Value1, string p_Value2)
{
Console.WriteLine(“Result ={0}”, p_Value1 + p_Value2); // This will perform concatenation of p_Value1 and p_Value2
}
}

(5) Inheritance

– Inheritance provides a way to inherit member of another class.
– It Provides a way to reuse code which is already written.

class InheritanceExample
{
public abstract class AllCar
{
public abstract string GetCarName();
}

public class HondaSeries : AllCar
{
//this method name matches with same signature/parameters in AllCar class
public override string GetCarName()
{
return “Car Name is Honda City”;
}
}

public static void Main(string[] args)
{
AllCar objAllCar = new HondaSeries();
Console.WriteLine(objAllCar.GetCarName());
Console.Read();
}
}

10 Best Chatting Apps In India

  The modern world is all about Internet. There was a time when people use to pay high telephone bills to stay in touch with their friends a...