Showing posts with label Design Pattern. Show all posts
Showing posts with label Design Pattern. Show all posts

Sunday, 14 August 2016

Passing Data from Controller to View ASP.NET MVC

Posted by : Manav Pandya
How to Pass Data from Controller to View ASP.NET MVC  ??



How to Pass Data from Controller to View ASP.NET MVC  ??



  • The MVC pattern stands for [ Model,  View and Controller ]. So, Model, View and Controller all distinct and separate but all work together to build MVC Applications.
  • The controller coordinate model and views to execute the application logic. The view display the data to the client.
  • There are two ways to pass the data from controller to view.

1.     ViewData.
2.     ViewBag.
  • Lets Create New ASP.Net MVC Application with Basic Template and Name it to “ViewBagDemo”.
  • Now, Add HomeController inside controller folder and add Index action as shown in below code.

 Code :

public class HomeController : Controller
{
  
    public ActionResult Index()
    {
        ViewBag.Name = "Manav Pandya";
        ViewBag.Description = "This is the Demo of ViewBag in ASP.NET MVC";
        return View();
    }

}


  • Now, right click on Index action -> Add View -> Leave all configuration -> click “Add”.
  • Now, add below line of code into Index.cshtml view to access ViewBag property in the view. 

Code :


@{
 ViewBag.Title = "Index";
}

<h2>Index</h2>

<h1>@ViewBag.Name</h1>
<div>@ViewBag.Description</div>


Now, Run the project and browse the Index action of HomeController. You will get output as shown in below screenshot.


Final Output :


Thanks for Reading ....

See my other article also :




ASP.NET , C# , Javascript , Bootstrap , AngularJs , Model View Controller , Java , Visual Studio , Ready made projects , MVC 4.0 , .NET Core
Read More

Wednesday, 6 July 2016

ASP.NET MVC Pattern

Posted by : Manav Pandya

ASP.NET MVC is a free and fully supported framework for building web applications that use the model-view-controller pattern. 

Like :  ASP.NET Web Forms,

 ASP.NET MVC is built on top of the ASP.NET Framework. This means you can use
in ASP.NET MVC applications the same APIs for security, state management, membership, caching, and so on that you could use in traditional ASP.NET Web Forms applications.




 [ 1 ]  Model 


The model in the MVC pattern represents the parts of the application that implement the data domain logic.

The operation of the model might come from the generation of classes representing objects in a data store such as a 
database (for example, Entity Framework data classes).


[ 2 ] View


Views are the visible elements in the application. They are the components that typically show users data from 
the model. 



A view page typically receives a view model object from the controller (the view doesn’t care how this data 
was obtained—that’s the controller’s responsibility!). 



The view page contains HTML (and possibly some UI-related 
code) to determine how to render the model data back to the browser .


[ 3 ] Controller



The controllers are classes that collect the user requests, work with the model, and ultimately select a view to 
render the appropriate UI.


Thanks Reader's for reading ....



ASP.NET , MVC , ASP.NET MVC , C# , dotnet , AngularJS , ASP , Sql Server , LINQ , Interview , .NET Core , SQL , LINQ to SQL
Read More