The inspiration for this blog on Razor was the Web Camp conducted by Microsoft Evangelists Nahas Mohammed and Ramaprasanna Chellamuthu on February 18 at Kochi. We received great insight into Razor View Engine and Web Matrix.
A few of the audience were invited to the stage and it was more of an interactive Code camp.
I am using Visual Web Developer 2010, a free tool from Microsoft for developing Asp .Net Applications. All the examples will work in Visual Studio. I have already installed Asp .Net MVC 3 in my machine.
Visual Studio 2010 / Visual Web Developer 2010 comes will Intellisense and debugging support for Razor. Unlike Asp .Net Web Forms in Razor the Visual Interface is defined using HTML.
I am starting an Empty Web Site and Adding an Empty Razor Page.
@{
var strName = "";
if(IsPost)
{
strName = "Hello welcome to Razor"
}
}
<form method=post>
<p>
@strName
</p>
<input id="Submit1" type="submit" value="submit" />
</form>
Inside a block each statement should end with a semicolon. Inside html you can call the memory variable using @ symbol. There semicolon should not be present.
We are checking a form post using IsPost. On Clicking the Submit buttom a Form POST will occur after that the memory variable will be assigned the Hello message and that message will be dispayed.
Using can use Html Tab in the Toolbox to add Html tags to the form.
You can see that the auto completion feature is available.
@{
var strName = "";
if(IsPost)
{
strName = "Hello " + Request["txtName"];
}
}
<form method=post>
<p>
@strName
</p>
Name
<input Name="txtName" type="text" />
<br />
<input id="Submit1" type="submit" value="submit" />
</form>
In the above example we extracting the value of textbox using Request.@{
var sum = 0;
if(IsPost)
{
sum = Request["txtValue1"].AsInt() + Request["txtValue2"].AsInt();
}
}
<form method =post>
<p>
@sum
</p>
Value 1<input Name = "txtValue1" type="text" />
</br>
Value 2<input Name = "txtValue2" type="text" />
</br>
<input id="Submit1" type="submit" value="submit" />
</form>
AsInt() is used to convert the value to Integer.
Session
//Session.cshtml
@{
Session["Name"] = "Shalvin";
}
<form method =post>
<a href = "@Href("Welcome")">Welcome</a>
</form>
//Welcome.cshtml
@{
var strName = "Hello " + Session["Name"];
}
@strName
Href method is used to create paths to resources like Web pages.
In the first page we are defining the session variable and in the second page we extracting the value of Session variable.
Entity Framework
I am creating an Entity Data Model is shown in the figure
@{
var context = new ContactManagementModel.ContactManagementEntities();
var Grps = context.ContactGroups;
var gd = new WebGrid(Grps);
}
<html>
<body>
@gd.GetHtml();
</body>
</html>
WebGrid helper can be used to display day is a tabular manner.
@{
var context = new ContactManagementModel.ContactManagementEntities();
var strMessage = "";
if(IsPost)
{
var Grp = new ContactManagementModel.ContactGroup{GroupName = Request["txtGroupName"]};
context.AddToContactGroups(Grp);
context.SaveChanges();
strMessage = "Record Saved";
}
}
<form method = "post">
<p>
@strMessage
</p>
Group Name
<input Name = "txtGroupName" type="text" />
</br>
<input id="Submit1" type="submit" value="submit" />
</form>
No comments:
Post a Comment