Posts

MVC 4 - Example sending email - WebMail

This is a example of sending email snippet code in Razor Editor. Sample Sending Email - Razor @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Thanks</title> </head> <body> @{ try { WebMail.SmtpServer = "smtp.example.com"; WebMail.SmtpPort = 587; WebMail.EnableSsl = true; WebMail.UserName = "mySmtpUsername"; WebMail.Password = "mySmtpPassword"; WebMail.From = "rsvps@example.com"; WebMail.Send("party-host@example.com", "RSVP Notification", "Message Send will be here"); }  catch (Exception)  { @:<b>Sorry - we couldn't send the email to confirm your RSVP.</b> } } <div> </div> </body> </html> By Mohd Zulkamal NO

MVC 4 - Adding Validation in a form

Image
This is a example of adding a validation in MVC form. Please refer to this post for reference of this example. Adding error message and validation rule GuestResponse.cs using System; using System.Collections.Generic; using System.Web; using System.ComponentModel.DataAnnotations; namespace MyFirstMVCApp.Models {     public class GuestResponse     {         [Required(ErrorMessage = "Please enter your name")]         public string Name { get; set; }         [Required(ErrorMessage = "Please enter your email address")]         [RegularExpression(".+\\@.+\\..+",         ErrorMessage = "Please enter a valid email address")]         public string Email { get; set; }         [Required(ErrorMessage = "Please enter your phone number")]         public string Phone { get; set; }         [Required(ErrorMessage = "Please specify whether you'll attend")]         public bool? WillAttend { get; set; }     } } HomeController.cs         [H

MVC 4 - Creating simple Data-Entry Application

This example will show how to create simple data entry form in MVC4 asp.net. Step By Step Creating Data Entry Application File >> New Project Choose ASP.NET MVC4 Web Application . (If you dont have MVC 4, Install it here ) On Project Template , choose Empty if you want to start from zero or choose Internet Application to start developement with default template. Choose View engine Razor. Click OK button  Create HomeController.cs under Controller Folder. Create GuestResponse.cs Model Create RsvpForm , Thanks , and Index View under View >> Home folder. See the file code example below. HomeController.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using MyFirstMVCApp.Models; namespace MyFirstMVCApp.Controllers {     public class HomeController : Controller     {         //         // GET: /Home/         public ViewResult Index()         {             return View();         }         public ViewResult RsvpForm

MVC 4 - Dynamic Output

Image
The whole point of a Web application platform is to construct and display dynamic output. In MVC, it is the controller’s job to construct some data and pass it to the view, which is responsible for rendering it to HTML. One way to pass data from the controller to the view is by using the ViewBag object, which is a member of the Controller base class. ViewBag is a dynamic object to which you can assign arbitrary properties, making those values available in whatever view is subsequently rendered. Example below demonstrates passing some simple dynamic data in this way in the HomeController.cs file. HomeController.cs  public class HomeController : Controller     {         //         // GET: /Home/         public ViewResult Index()         {             int hour = DateTime.Now.Hour;             ViewBag.Greeting = hour < 12 ? "Good Morning" : "Good Afternoon";             return View();         }     } Index.cshtml @{ Layout = null; } <!DOCTYPE html> <html&g

MVC 4 - Rendering web Pages

Image
The output from the previous post wasn’t HTML—it was just the string " Hello World, This is my first MVC Application ". To produce an HTML response to a browser request, we need to create a view. Creating And Rendering a View Modify the Controller to render a View (please refer the previous post before you do this).         public ViewResult Index()         {             return View();         } Right click at word View() and choose Add View Leave the devault value and click Add button Congratulation, now you have successfully created new view which will be rendered as html when the root / or  /Home or /Home/Index url hit Note : The . cshtml file extension denotes a C# view that will be processed by Razor . Now copy this HTML code on the Index.cshtml file. @{     ViewBag.Title = "Index"; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head>

Creating Your First MVC 4 Application

Image
In this tutorial i will show the example on MVC 4. Follow this step to create new MVC Application on Microsoft Visual Studio 2010. Creating MVC 4 Project File >> New Project Choose ASP.NET MVC4 Web Application. (If you dont have MVC 4, Install it here ) On Project Template , choose Empty if you want to start from zero or choose Internet Application to start developement with default template. Choose View engine Razor. Click OK button Now you have create one project with MVC4 Solution  Adding First Controller Right click on the Controller folder under Solution Explorer Menu. Choose Add >> Controller Rename Controller Name to HomeController Since this is empty project, so under Scaffolding Options Template choose Empty MVC Controller Change The default ActionResult Index() like this         public string Index()         {             return "Hello World, This is my first MVC Application";         } Run the project, You should see the output on the browser lik

Decimal Manipulation in C#

This post will  show manipulation on the Data Type Decimal. //This is the hard code value for decimal   //without suffic m or M, the compiler thread the object as a double hence will cause error   decimal price = 10.1654M;   //Convert decimal to double   double priceInDouble = Convert.ToDouble(price); //output : 10.1654   priceInDouble = (double)price;//output : 10.1654   //convert to double with 2 decimal point,           priceInDouble = Math.Round((double)price, 2); // output : 10.17   //convert back to decimal   price = Convert.ToDecimal(priceInDouble);//output : 10.17   price = (decimal)priceInDouble;//output : 10.17   //convert decimal to currency. The currency format will depend on the region,   //like myself in malaysia compiler will automatically give "RM" as a currency format   string currency = String.Format("Order Total: {0:C}", price); //output :RM10.17   currency = String.Format("Order Total: {0:C2}", price); //output :RM10.17 ,2 decimal poi