Friday, December 3, 2010

ASP.NET MVC Razor IsPost property with IF –Else loop

ASP.NET MVC Razor a new view engine from Microsoft looks very promising. Here are example of code where we can determine page is post back or not. It support a IsPost Property which will tell you whether page is post back or not. So based on that we can write code for handling post back. Also one of greatest feature of razor is we can write code for decision making like if else and other stuff with single @ statement isn’t that great stuff.

Here is the stuff how we can write the code with IsPost property.

@{
var Message="";
if(IsPost)
{
Message ="This is from the postback";
}
else
{
Message="This is without postback";
}
}
And we can now print that variable with following HTML Form.
<form method="POST" action="" >
<input type="Submit" name="Submit" value="Submit"/>
<p>@Message</p>
</form>

Here submit button will submit the form and based on that it will print message. Let’s see how it looks in browser before post back and after post back.

WithoutPostback

And After post back

AfterPostaback

So that’s it. Now you can do lots of stuff with IsPost property possibilities are unlimited!!. Hope this will help you..Happy Programming.

Technorati Tags: ,,
Shout it
kick it on DotNetKicks.com
Share:

4 comments:

  1. Is this Same as IsPostBack in asp.net
     

    ReplyDelete
  2. I am new to MVC. your Blogs are really amazing to me. i wonder if you could share any example of a complete CMS, Blog to be written in MVC 5

    ReplyDelete
  3. There are lots of CMS available in asp.net mvc like umbraco, orachard etc. Search about it on internet and you will find lots of examples of it.

    ReplyDelete

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews