European ASP.NET MVC 4 and MVC 5 Hosting

BLOG about ASP.NET MVC 3, ASP.NET MVC 4, and ASP.NET MVC 5 Hosting and Its Technology - Dedicated to European Windows Hosting Customer

ASP.NET MVC 5 Hosting UK - HostForLIFE.eu :: RegularExpression Validation Using Annotations in ASP.NET MVC 5

clock October 21, 2014 11:34 by author Peter

We can do Regular Expression or REGEX validation using Annotations in ASP.NET MVC 5. we can use "RegularExpression" attribute for validation. during this attribute we specify Regular Expression string. We can also specify our custom error messages for that we'd like to set "ErrorMessage" Property in "RegularExpression" attribute. Here is the example for this.

We take "Email" Address validation using REGEX. If user enter email address, which isn't in correct format at that time our validation message display on screen.
[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}",
        ErrorMessage="Email Address is not in proper format.")]
        public string Email { get; set; }

At the view side , add this code:
  @Html.EditorFor(model => model.Email)
        @Html.ValidationMessageFor(model => model.Email,"", new {  style="color:red"})

Here is Code for this. Model (Customer.cs):

public class Customer
    {
        public int CustomerID { get; set; }
        [Required]
        [StringLength(20,MinimumLength=2)]
        public string FirstName { get; set; }
        [StringLength(10)]
        public string LastName { get; set; }
        [RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}",
        ErrorMessage="Email Address is not in proper format.")]
       public string Email { get; set; }
    }



ASP.NET MVC 6 Hosting - HostForLIFE.eu :: Displaying Validation Errors with ASP.NET MVC

clock September 9, 2014 09:33 by author Peter

We all know how important it is to validate user input, but it’s equally important how we present validation errors to the user. Take the following form you can see below:

The email field needs to be optional but any address entered must be valid. If a validation error occurs, it should replace the optional label with the error message. Behind the scenes I’m using Data Annotations to validate the email address, so any errors will be passed into the ModelState. ASP.NET MVC 6 has built-in support for showing these errors (using the ValidateMessageFor helper) so it’s easy to write a wrapper around that, supplying the ‘optional’ text as a parameter.

Here’s an extension method for the HtmlHelper:
public static class ExtensionMethods

{
 public static MvcHtmlString HelpMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string helpText)
{
        var validation = htmlHelper.ValidationMessageFor(expression);
        if (validation == null)
            return new MvcHtmlString("<span class='help-inline muted'>" + helpText + "</span>");
        else
            return validation;
    }
}

Here’s it’s usage:

<fieldset>
  <div class="control-group">
        @Html.LabelFor(x => x.EmailAddress, "Email", new { @class = "control-label" })
        <div class="controls">
            @Html.TextBoxFor(x => x.EmailAddress, new { @class = "input-xlarge", placeholder = "Enter an email address" })
            @Html.HelpMessageFor(x => x.EmailAddress, "Optional")
        </div>
    </div>
</fieldset>



European ASP.NET MVC 5 Hosting - UK :: MVC Controllers with Visual Studio 2013 and ASP.NET MVC 5

clock August 6, 2014 09:14 by author Onit

Introduction

Before we start this article we will give you a quick reviews about MVC there are three parts to MVC.

Models: Part of the application that handles the application logic and contains classes representing data structure.

Views: Part of the application that handles the generation of HTML responses

Controllers: Part of the application that handles user interaction and incoming browser requests, retrieves model data and specify views

In Getting Started with Visual Studio 2013 and ASP.NET MVC 5 we created a new MVC application and took a look at some of the basics.

In this blog post we will take a look at Controllers and how they can be used

Controller in MVC

Adding a new controller The ASP.NET MVC framework maps URLs to classes that are referred to as controllers. Controllers process incoming requests, handles user input, interactions and executes appropriate application logic. A controller class typically calls a separate view component to generate the HTML mark-up for the request. The base class for all controllers is the ControllerBase class. The Controller class inherits from the ControllerBase and is the default implementation of a controller. In an MVC application, the Controller handles the following areas:

  1. Locating and validating the necessary action method.
  2. Getting any values used in the action method’s arguments.
  3. Handling any errors that occurs.
  4. Providing the deault WebFormViewEngine class for rendering views.

Using the solution we built in our previous post let’s add a new controller using these steps.

  • In the Solution explorer right click on the Controllers folder, select Add and then Controller as shown in the following screenshot.

  • In the Add Scaffold box select MVC 5 Controller – Empty and then click Add as shown in the following screenshot.

  • In the Add Controller dialog box in the Controller name field enter TopicController and then select Add as shown in the following screenshot.

  • Validate in the Solution Explorer in the Controllers folder that you have the new TopicController.cs file, in the Views folder you should also have the Topic folder as shown in the following screenshot.

 

** Note! there is a folder in the Views, while it is not required Views and Controllers are usually tied together. When you name a new Controller with the suffix “Controller”, Visual Studio will create the View folder automatically
In the Project explorer make sure that you have the TopicController.cs selected and in the code window replace it with the following code.

using System.Web;
using System.Web.Mvc;
 
namespace MvcMovie.Controllers
{
    public class TopicController : Controller
    {
        //
        // GET: /Topic/
 
        public string Index()
        {
            return "This is the <b>default</b> action...";
        }
 
        //
        // GET: /Topic/Welcome/
 
        public string Welcome()
        {
            return "This is the Welcome action method...";
        }
    }
}

Accessing the Controller

If the traditional ASP.NET Web Form application have user interactions organized around pages, raising and handling events from these pages and their form controls, then MVC applications are organized around controllers and action methods. A controller contains actions methods that typically have a one-to-one mapping with user interactions. For example, entering a URL into a browser causes a request to the server. The MVC application uses routing rules defined in the Global.asax file to parse the URL and to determine the path of the controller. The controller then determine the appropriate action method to call to handle the request.

By default a web request in an MVC application is treated as a sub-path that includes the controller names followed by the action name. For example is a user enters the

URL:http://www.yourdomain.com/product/category/1

The sub path evaluated is product/category/1. The default routing rule will treat product as the prefix name of the controllers (which must end in Controller). It will treat Category as the name of the action. In this case the routing rule will invoke the category method of the product controller in order to process the request. By default the value of 5 in the URL will be passed to the Detail method as a parameter.

Take a look at how this works with our application.

  • Press F5 to start the application and validate that you see a similar URL as shown in the following screenshot

  • Append the URL of the application with the string /Topic and then refresh the screen as shown in the following screenshot

  • Append the URL of the application with the string /Topic/Welcome and then refresh as shown in the following screenshot

 



About HostForLIFE.eu

HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.

We have offered the latest Windows 2016 Hosting, ASP.NET Core 2.2.1 Hosting, ASP.NET MVC 6 Hosting and SQL 2017 Hosting.


Tag cloud

Sign in