Posts

MVC - URL Routing

Image
   The segments in an examle URL The first segment contains the word Admin , and the second segment contains the word Index . To the human eye, it is obvious that the first segment relates to the controller and the second segment relates to the action. But, of course, we need to express this relationship in a way that the routing system can understand. Here is a URL pattern that does this: {controller}/{action} or {controller}/{action}/id or {controller}/{action}/id?querystring2=&querystring3=  Note : id, querystring2, and querystring 3 is a parameter in the controller . When processing an incoming request, the job of the routing system is to match the URL that has been requested to a pattern and extract values from the URL for the segment variables defined in the pattern. The segment variables are expressed using braces (the { and } characters). The example pattern has two segment variables with the names controller and action , and so the value of the controller segment variable

Understanding the ASP.NET MVC Framework Pattern

Image
In high-level terms, the MVC pattern means that an MVC application will be split into at least three pieces: Models , which contain or represent the data that users work with. These can be simple view models, which just represent data being transferred between views and controllers; or they can be domain models, which contain the data in a business domain as well as the operations, transformations, and rules for manipulating that data. Views , which are used to render some part of the model as a user interface. Controllers , which process incoming requests, perform operations on the model,and select views to render to the user. Models are the definition of the universe your application works in. In a banking application, for example, the model represents everything in the bank that the application supports, such as accounts, the general ledger, and credit limits for customers as well as the operations that can be used to manipulate the data in the model, such as depositing funds and ma

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>