Getting Started with ASP.NET MVC Chat UI control

21 Jul 20266 minutes to read

This section briefly explains how to include the ASP.NET MVC Chat UI control in your ASP.NET MVC application using Visual Studio.

Prerequisites

.NET and Visual Studio compatibility

.NET Version Visual Studio Version
.NET Framework 4.6.2 Visual Studio 2015 Update 3 or later

Browser support

Browser Versions
Google Chrome, including Android & iOS Latest Version
Mozilla Firefox Latest Version
Microsoft Edge Latest Version
Apple Safari, including iOS Latest Version
Opera Latest Version
Microsoft Internet Explorer 11

Create an ASP.NET MVC Web App with HTML Helper

Create an ASP.NET MVC Web App using Visual Studio via Microsoft Templates or the Syncfusion® ASP.NET MVC Extension. For detailed instructions, refer to the ASP.NET MVC Getting Started documentation.

Install the required ASP.NET MVC package

To add ASP.NET MVC Chat UI control in the app, open the NuGet package manager in Visual Studio (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), search for and install the Syncfusion.AspNetMvc.InteractiveChat package. All Syncfusion ASP.NET MVC packages are available in nuget.org. See the NuGet packages topic for details.

Alternatively, you can install the same package using the Package Manager Console with the following command.

Install-Package Syncfusion.AspNetMvc.InteractiveChat -Version 34.1.29

Add the Namespace

After the package is installed, open the ~/Views/Web.config file and import the Syncfusion.EJ2 namespace.

<namespaces>
    <add namespace="Syncfusion.EJ2" />
</namespaces>

Add stylesheet and script resources

The theme stylesheet and script are referenced from the CDN. Include the stylesheet and script references inside the <head> of the ~/Views/Shared/_Layout.cshtml.

<head>
    ...
    <!-- ASP.NET MVC controls styles -->
    <link rel="stylesheet" href="https://cdn.syncfusion.com/ej2/34.1.29/fluent2.css" />
    <!-- ASP.NET MVC controls scripts -->
    <script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js"></script>
</head>

Register the script manager

Open the ~/Views/Shared/_Layout.cshtml file and register the script manager EJS().ScriptManager() at the end of the <body> element as follows.

<body>
...
    <!-- ASP.NET MVC Script Manager -->
    @Html.EJS().ScriptManager()
    
</body>

Add ASP.NET MVC Chat UI control

Add the ASP.NET MVC Chat UI control in the ~/Views/Home/Index.cshtml file.

@using Syncfusion.EJ2.InteractiveChat

<div class="chatui-container" style="height: 400px; width: 400px;">
    @Html.EJS().ChatUI("chatUI").User(ViewBag.CurrentUser).Render()
</div>
using Syncfusion.EJ2.InteractiveChat;

public ChatUIUser CurrentUser { get; set; }
public ChatUIUser CurrentUserModel { get; set; } = new ChatUIUser() { Id = "user1", User = "Albert" };

public ActionResult Default()
{
    CurrentUser = CurrentUserModel;
    ViewBag.CurrentUser = CurrentUser;
    return View();
}

Run the application

Press Ctrl+F5 (Windows) or +F5 (macOS) to launch the application. The ASP.NET MVC Chat UI control will render in your default web browser.

ASP.NET MVC Chat UI Control

Configure messages and user

You can use the Messages property to add messages and the User property to configure the current user for the chat.

@using Syncfusion.EJ2.InteractiveChat

<div class="chatui-container" style="height:380px; width:450px">
    @Html.EJS().ChatUI("chatUser").Messages(ViewBag.ChatMessagesData).User(ViewBag.CurrentUser).Render()
</div>
using Syncfusion.EJ2.InteractiveChat;

public ChatUIUser CurrentUser { get; set; }
public List<ChatUIMessage> ChatMessagesData { get; set; } = new List<ChatUIMessage>();
public ChatUIUser CurrentUserModel { get; set; } = new ChatUIUser() { Id = "user1", User = "Albert" };
public ChatUIUser MichaleUserModel { get; set; } = new ChatUIUser() { Id = "user2", User = "Michale Suyama" };
 
public void OnGet()
{
 
    CurrentUser = CurrentUserModel;
    ChatMessagesData.Add(new ChatUIMessage()
    {
        Text = "Hi Michale, are we on track for the deadline?",
        Author = CurrentUserModel
    });
    ChatMessagesData.Add(new ChatUIMessage()
    {
        Text = "Yes, the design phase is complete.",
        Author = MichaleUserModel
    });
    ChatMessagesData.Add(new ChatUIMessage()
    {
        Text = "I’ll review it and send feedback by today.",
        Author = CurrentUserModel
    });
 
}

ASP.NET MVC Chat UI default messages