Category

Category: ASP.NET

Consuming WCF in ASP.NET MVC 5 with Entity Framework 6 using Visual Studio 2017

This article includes a step by step tutorial on how to create a WCF Service and a consumer application ASP.NET MVC 5. We will be using Entity Framework for database operations.

Terminologies:

  • WCF – Windows Communication Foundation is a framework for building service-oriented applications. It allows different applications to interact with each other via a service provider.
  • MVC –  Model-View-Controller – a design pattern for successfully and efficiently relating the user interface to underlying data models.
  • Entity Framework – is an open source object-relational mapping framework.
  1. Create a blank solution in Visual Studio, name it MoviesApp
  2. Create a WCF Application
    • Right click on the MoviesApp on the Solution Explorer then go to Add -> New Project.
    • Select WCF Service Application then name it WCF. Click Ok
  3. After the project is successfully created, we now add reference to 3 DLL files from the MySQL Connector directory:
    Go to the Solution Explorer then right click on Reference then click Add Reference. Refer to the screenshot bellow:

    • A window will pop-up. The the left side pain, click on “Browse” then locate the 3 DLL files as seen from the
      screenshot bellow. The path to the DLL files is: C:\Program Files (x86)\MySQL\Connector NET 8.0\Assemblies\v4.5.2. When you’re done finding the files, make sure to check all 3 files before clicking the “OK” button.
  4. Install Entity Framework to the project using Nuget Package manager. Follow the screenshot bellow:
    • On the Nuget Package Manager window, click on “Browse” then search for “entity framework”. Select EntityFramework from the results then on the right side pain check the box of your application then click on the “Install” button. Refer to the screenshot bellow:
  5. Open MySQL Workbench then create a new schema, name it anything you want. Then inside the schema create a new table called “movies” with the following columns:
  6. ….

Click here to download the FULL TUTORIAL

Connecting an ASP.NET MVC Application to MySQL

In this tutorial you are going to learn how to connect an ASP.NET MVC application to MySQL. We will also be creating a CRUD application that utilizes MySQL as data source and Entity Framework as object mapper. So let’s get started!

  1. Download and Install MySQL Installer 5.7 from this link: https://dev.mysql.com/downloads/windows/installer/
    • After installation, open MySQL Installer then download the following softwares:
      1. MySQL Server (v5.7)
      2. MySQL WorkBench
      3. MySQL For Visual Studio
      4. MySQL Connector/NET
    • After installation of above 4 softwares, make sure to verify by checking Programs & Features under Control Panel. Refer to the image bellow.
  2. Create a project. Go to Visual Studio -> File -> New -> Project.
    • From the pop-up, make sure the following are selected. Click the “Ok” button when you’re done.
    • In the next screen, make sure to select MVC. Click “Ok” button when you’re done.
  3. After the project is successfully created, we now add reference to…..

Click here to download the FULL TUTORIAL

Ajax Multi File Upload in ASP.NET MVC 5

In this tutorial you will learn how to create a multi-file upload form that submits via Ajax. The plan is to store the files in a folder located in your server and save the file names in single database field named “images” with the format of a JSON string, like this: [“image_101.jpg”,”image_102.jpg”,”image_103.jpg”,”image_104.jpg”,”image_105.jpg”, …. ] but if you do not like this format you can still use this tutorial, you just need to do a few changes in the controller to fit your web app requirements.

Let’s get started.

1. Include the CDN script bellow into the footer part of your application (after jquery). This is the plugin that we’ll be using for submitting files asynchronously via Ajax.

1
@Scripts.Render("//cdnjs.cloudflare.com/ajax/libs/jquery.form/3.51/jquery.form.min.js")

2. In your razor view, add the following form:

1
2
3
4
5
6
7
8
9
10
11
12
@using (Html.BeginForm("Index", "Upload", new { id = @Model.id }, FormMethod.Post, new { @class = "upload-images" }))
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(model => model.id)
       
    <div class="form-group">
        @Html.LabelFor(model => model.images, "Select Images")
        <input type="file" name="images[]" accept="image/*" class="form-control" multiple />
    </div>
       
    <input type="submit" class="btn btn-success" value="Upload" />
}

3. Then in your controller, include the the method bellow.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
[HttpPost]
[ValidateAntiForgeryToken]
public string Index([Bind(Include = "images")] Product product)
{
    /* Let's put our variables in a dictionary */
    var response = new Dictionary<string, string> {
        { "status", "0" },
        { "message", "" },
        { "images", "" }
    };

    if (ModelState.IsValid)
    {
        /* Image uploade handler */
        try
        {
            HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files; /* Retrieve files from form request */
            string path = "/Content/Images/Uploads/"; /* Location to store images */
            List<string> images = new List<string>(); /* Initialize an empty list for our images */

            /* Check if Products Directory exists */
            if (!Directory.Exists(path))
            {
                /* Create Products directory if not exists */
                Directory.CreateDirectory(Server.MapPath("~") + path);
            }

            /* Process each image */
            for (int i = 0; i < hfc.Count; i++)
            {
                HttpPostedFile hpf = hfc[i];
                if (hpf.ContentLength > 0)
                {
                    string file_name_original = Path.GetFileNameWithoutExtension(hpf.FileName); /* Original file name with extension */
                    string file_name = GlobalMethods.GenerateRandomString(20); /* Generate a random string for our image name */
                    string file_extension = Path.GetExtension(hpf.FileName); /* Get file extension */
                    string file_path_with_file_name = path + file_name + file_extension; /* Final image path */

                    /* Append single image to our images list */
                    images.Add(file_name + file_extension);

                    /* Move files to the specified directory on the "path" variable */
                    hpf.SaveAs(Server.MapPath(file_path_with_file_name));
                }
            }

            /* If there is an image uploaded, let's include them to the response dictionary */
            if (images.Count > 0)
            {
                if (!String.IsNullOrEmpty(original_data_detached.images))
                {
                    /* Get existing images from database then convert it to an array */
                    var old_images_array = new JavaScriptSerializer().Deserialize<dynamic>(original_data_detached.images);

                    /* Add old images to the new images list variable*/
                    foreach (var image in old_images_array)
                    {
                        images.Add(image);
                    }
                }

                /* Convert images list to json */
                response["images"] = new JavaScriptSerializer().Serialize(images);

                product.images = response["images"];
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
       
        db.Entry(product).State = EntityState.Modified;

        /* Do not update images field if there are no new posted images */
        if (String.IsNullOrEmpty(product.images))
        {
            db.DontUpdateProperty<Product>("images");
        }

        db.SaveChanges();
        response["status"] = "1";
        response["message"] = "Successfully Updated";
    }

    /* Then we return the Dictionary in json format to our front-end */
    string json = new JavaScriptSerializer().Serialize(response);
    return json;
}

Note: You will have to change the model names above to fit in your application. But if you want to see my model implementation, here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
namespace MyApp.Models
{
    public class Product
    {
        public int id { get; set; }
        public string images { get; set; }
    }

    public class ProductDBContext : DbContext
    {
        public DbSet<Product> Products { get; set; }
    }
}

4. Create the file: ./Scripts/app.js then add the code bellow. This will handle our ajax submit. Make sure to include this file in the footer part of your application (AFTER jquery and jquery.form)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$(document).ready(function () {
    $('.upload-images').ajaxForm({
        success: function (response, textStatus, xhr, form) {
            response = JSON.parse(response);
                                         
            if (response.status == 1) {
                    alert(response.message);
                }
            }
            if (response.images) {
                $.each(JSON.parse(response.images), function (index, image) {
                    /* Code what to do with the returned images */
                });
            }
        }
    });
});

5. That’s it. now test your web app.

AJAX Pagination with Search and Sort in ASP.NET MVC 5

In this tutorial, we are going to build a simple AJAX Pagination with search and sort using Entity Framework with ASP.NET MVC. This tutorial assumes that you already have an existing ASP.NET MVC web application built using Visual Studio 2013 or 2015. What you see in the above picture is exactly what we are going to build out. So let’s get started.

In Visual Studio, create a new Model class under your Models folder and name it ProductModel.cs. In it add the following code:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MyAppName.Models
{
    public class Product
    {
        public int id { get; set; }
        public string name { get; set; }
        [AllowHtml]
        public string content { get; set; }
        public string excerpt { get; set; }
        public DateTime? date { get; set; }
        public decimal price { get; set; }
        public int quantity { get; set; }
        public int status { get; set; }
        public int author { get; set; }
        public string images { get; set; }
        public string featured_image { get; set; }
    }

    public class ProductDBContext : DbContext
    {
        public DbSet<Product> Products { get; set; }
    }
}

From the Solution Explorer of Visual Studio, right click on the Models Folder -> Add -> New Scaffolded Item

A new window will pop-up, select MVC 5 Controller with views, using Entity Framework then click Add.

On the next window: Under Model class select Product(MyAppName.Models), for the Data context class select ProductDBContext (MyAppName.Models). If you have an existing razor layout, you can use it as the new view for our Products scaffold. Click the Add button when you’re done. This will start generating the necessary views and controller for our Products.

We can now start working on the pagination.

Navigate to View -> Products -> Index.cshtml then replace the Index() method with the following….

Click here to download the FULL TUTORIAL