Support us on YouTube by Subscribing our YouTube Channel. Click here to Subscribe our YouTube Channel

Tuesday, 26 May 2015

Passing Data from Controller to View in ASP.NET MVC

In ASP.Net MVC, We can pass data from the controller to corresponding view using ViewData and ViewBag. We can also pass data from the View to Controller using Post/ QueryString/ HiddenField and We can pass data from Controller to Controller using TempData.

In this post, we will learn How to Pass Data from Controller to View in ASP.NET MVC using ViewData and ViewBag. In the previous post, we saw How to Pass parameter or Query String in Action Method in ASP.NET MVC.

1. ViewData:
ViewData is a dictionary object used to pass data between from the controller to view in the form of Key-Value pair. ViewData is derivative of the ViewDataDictionary class. ViewData is introduced in MVC 1.0. ViewData requires type casting for complex data types and if redirection occurs, its value becomes NULL.

Example of ViewData: 
Create a New Empty ASP.Net MVC Project and Add a Controller named as HomeController in it.
Store the current DateTime in ViewData object and return a View. Now add a View(named as Index).
public ActionResult Index()
{
      ViewData["DateTime"] = DateTime.Now;
      return View();
}

Now call the ViewData["DateTime"] object in View using @ symbol because we have selected the Razor View Engine.
Index.cshtml Code:
@{
    ViewBag.Title = "Index";
}

<h2>ViewData Example</h2>
<p>@ViewData["DateTime"]</p>
Preview:

2. ViewBag:
ViewBag is a dynamic property(a new C# 4.0 feature) which means it has no pre-defined properties. ViewBag is a wrapper around the ViewData that exposes the ViewData dictionary as a dynamic object. ViewBag is slower than ViewData and it does not require any type of typecasting for the complex data type. Visual Studio cannot provide Intellisense support for the dynamic objects so error not shown at the design time.

Example of ViewBag:
Inside HomeController, Add another Action Method(named it as Demo). Store the current DateTime in ViewBag.DateTime dynamic object and return a View.
public ActionResult Demo()
{
       ViewBag.DateTime = DateTime.Now;
return View();
}
Now add a View(named as Demo).
Demo.cshtml Code:
@{
    ViewBag.Title = "Index";
}

<h2>ViewData Example</h2>
<p>@ViewData["DateTime"]</p>
Preview:
Hope you like it. Thanks.
[Download Source Code via Google Drive]

Monday, 18 May 2015

Create First Windows Phone 8 Application

In this tutorial, we will learn How to create First Application for Windows Phone 8. In previous tutorial, we saw How to install Windows Phone 8 SDK.

Let's Begin:
Create a New Windows Phone App.
Select Windows Phone OS 8.0 as a target Windows Phone OS for this application and click on OK.
Before starting, Let's take a look at project structure. In Solution Explorer, you will see the structure of the project.
Windows Phone Project contains Properties, References, Assests, Resources, App.xaml, MainPage.xaml and few additional files. App.xaml file is the starting point of Windows Phone Application and controls initializing of the application. Assets folder contains graphics, icons for the application. WMAppManifest.xml contains metadata for the application and plays important role Windows Phone App validation and certification. Resources folder contains the file that is used for Localization. MainPage.xaml is the main page that is displayed after launching the application.

Let's change the name of application and the title of the page. Go to MainPage.xaml file and change the Text property of TextBlock control.
I have changed Application title to My First Application and Page Title to Hello World.
Find the Grid named as ContentPanel in MainPage.xaml code and add TextBox and Button Control in it.

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBox x:Name="txt_Hello"
                     VerticalAlignment="Top">
            </TextBox>
            <Button x:Name="btn_HelloWorld"
                    VerticalAlignment="Top"
                    Content="Click Me!"
                    Margin="0 70 0 0"
                    Click="btn_HelloWorld_Click">
            </Button>
</Grid>
Preview:
Double click on btn_HelloWorld control to create click event and add the following lines of code:
private void btn_HelloWorld_Click(object sender, RoutedEventArgs e)
{
    if (txt_Hello.Text == "")
    {
         MessageBox.Show("Please enter some text in TextBox");
    }
    else
    {
         MessageBox.Show("Hello "+txt_Hello.Text);
    }
}
Build the solution and run the application in Windows Phone Emulator.
Final Preview:
I hope you like it. Thanks.
[Download Source Code via Google Drive]

Thursday, 7 May 2015

Pass parameter or Query String in Action Method in ASP.NET MVC

Before proceeding, I recommend you to read the previous article of this series
(Create First Application in ASP.NET MVC 4.0). In this Article, We will learn How to Pass parameter or Query String in an Action Method in ASP.NET MVC.

Let's Begin:
1. Create a New ASP.NET MVC4 Empty Project.
2. Add a controller -> Named it as HomeController -> Add Index Action Method to it. If you don't know How to create or How to add controller Read it here.
3. Pass parameter in Index Action Method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PassParameter.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public string Index(string id)
        {
            return "ID =" + id;
        }

    }
}
In the above code, we have passed a parameter named as the id of string data type. Now Build & Run the application and open http://localhost:13923/Home/Index/10 link in the URL by passing the parameter to Index Action Method of Home Controller.
Preview:
Pass parameter or Query String in Action Method

Passing Multiple Parameter in Index Action Method:
In the above example, we saw How to pass single parameter in Action Method But there are many situations when we have to pass more than one parameter. Let's see How to do this in ASP.NET MVC.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PassParameter.Controllers
{
    public class HomeController : Controller
    {

        public string Index(string id, string name)
        {
            return "ID =" + id+"<br /> Name="+name;
        }

    }
}
In the above code, we have added/passed multiple parameter (two) in Index Action method. Name of parameter in the URL must match with the Name of the parameter in the Action Method.
Preview:
We can also get the QueryString values using Request.QueryString Collection.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PassParameter.Controllers
{
    public class HomeController : Controller
    {

        public string Index(string id)
        {
            return "ID =" + id+"<br /> Name="+Request.QueryString["name"].ToString();
        }

    }
}
Preview:
I hope you enjoyed this post. If you have any query comment it below. 
[Download Source Code via Google Drive]

Subscribe us on YouTube

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Like us on Facebook