18 April 2014

Views and UI rendering in ASP.NET MVC

  The ASP.NET MVC framework supports view engine to generate views (UI). 

Default view page Index.cshtml

@{
    ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
<p>
    To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>

Layout page (_Layout.cshtml)

<! DOCTYPE html>
<Html>
<Head>
<title>@ViewBag.Title</title>
<link href="@Url.Content ("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content ("~/Scripts/jquery-1.5.1.min.js")" type="text/JavaScript"></script>
</head>
<Body>
<div class="page">
<div id="header">
<div id="title">
<h1>New Project</h1>
</div>
<div id="logindisplay">
@Html.Partial ("_LogOnPartial")
</div>
<div id="menucontainer">
<ul id="menu">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
</ul>
</div>
</div>
<div id="main">
@RenderBody ()
</div>
<div id="footer">
</div>
</div>
</body>
</html>


Automatic Mapping of Action Method Parameters and Incoming Request 
 
        Consider our action method have some parameters, mapping of user request data to the parameter is done automatically in MVC framework. MVC framework examines the user request that if there is an HTTP request value with same name, then it maps the value automatically to the parameter of action method. 

Consider an example, suppose we have a Controller 'Register' and an action method 'Add' in 'Register' Controller.
 

Public ActionResult Add (int id)
{
ViewData ["RegisterNumber"] = id;
ReturnView ();
}

The default route mapping rule has the format.../ {Controller}/ {action}/ {id}
 

No comments:

Post a Comment