How to use IsPost property with IF-Else Condition in ASP.NET MVC Razor

No.of Views802
Bookmarked0 times
Downloads 
Votes0
By  jalpesh   On  05 Dec 2010 20:12:55
Tag : ASP.NET , ASP.NET MVC
Through this article, i will explain how to use the IsPost property with IF-Else Condition in ASP.NET MVC Razor
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Through this article, i will explain how to use the IsPost property with IF-Else Condition in ASP.NET MVC Razor.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.

Image Loading

And After post back

Image Loading

That's all.

Conclusion

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

 
Sign Up to vote for this article
 
About Author
 
jalpesh
Occupation-Software Engineer
Company-DotNetJaps
Member Type-Expert
Location-India
Joined date-08 May 2010
Home Page-http://www.dotnetjalps.com
Blog Page-http://www.dotnetjalps.com
I am jalpesh vadgamaa an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having experience of five year in Microsoft .NET Technology.I am working as Project Leader in Mid Size company.My work area comprises of Enterprise Level projects using ASP.NET and other Microsoft .NET Technologies.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top