Getting Data from Controller to View
- ViewData - Assign a value with a key in the controller and read the value using the key in the View page
public ActionResult Index()
{
ViewData["Message"] = "Hello, World";
return View();
}
- ViewBag - allows an object to dynamically have properties added to it. So with a ViewBag, you can use the “.” notation.
public ActionResult Index()
{
ViewBag.Message = "Hello, World";
return View();
}
- TempData - is another dictionary for storing temporary data like the “ViewBag“ but is deleted at the end of the HTTP request
// marks the specified key in the dictionary for retention
public void Keep(string key)
// returns an object without marking the key for deletion
public Object Peek(string key)
- Strongly-Typed View Model Object – WebFormViewEngine
<%@ Page Inherits="System.Web.Mvc.ViewPage" %>
Product Name: <%: Model.Name %>
- Strongly-Typed View Model Object – Razor
@model Mvc3App.Models.Product
Product: @Model.Name
Getting Data from View to Controller
- Traditional Approach - HttpRequestBase
- FormCollection Object
- Action Parameters
- Strongly type model binding to view
ActionResult() response types:
There are 11 different response types which can be sent to the end user :-
- ViewResult - Renders a specified view to the response stream
- PartialViewResult - Renders a specified partial view to the response stream
- EmptyResult - An empty response is returned
- RedirectResult - Performs an HTTP redirection to a specified URL
- RedirectToRouteResult - Performs an HTTP redirection to a URL that is determined by the routing engine, based on given route data
- JsonResult - Serializes a given object to JSON format
- JavaScriptResult - Returns a piece of JavaScript code that can be executed on the client
- ContentResult - Writes content to the response stream without requiring a view
- FileContentResult - Returns a file to the client
- FileStreamResult - Returns a file to the client, which is provided by a Stream
- FilePathResult - Returns a file to the client
0 comments:
Post a Comment