Click here to Skip to main content
15,885,767 members
Articles / Web Development / HTML
Tip/Trick

Modal Search and Select Item Dialog for ASP.NET

Rate me:
Please Sign up or sign in to vote.
3.33/5 (3 votes)
21 Apr 2017CPOL2 min read 17K   580   5  
Ajax search modal dialog with item selection

Introduction

I’m a long time C# developer and for our company internal system, I need to develop a web application. Of course, ASP.NET was the choice. I’m used to Windows Forms/Desktop applications and I miss a lot of features in web development.

Image 1

Modal dialog with select and search

The one feature I missed the most was the reusable modal dialog with search and select item option. I use it for customer, country or storage item selection (for invoices). Also, I required to use it without page refresh, that’s why Ajax and JavaScript is used.

Just one note – I’m not a web developer, I know probably something could be done easier/faster in the example. But it works ?? …

Using the Code

You need to perform the following steps to use the dialog.

Add jqueryui into _Layout.cshtml.

Razor
@Styles.Render("~/Content/themes/base/css")
@Styles.Render("~/Content/themes/base/jquery.ui.all.css")
@Styles.Render("~/Content/themes/base/jquery.ui.dialog.css")

Also, you need to add jQuery CSS into layout, otherwise the dialog will not be displayed correctly (transparent, big, ...).

Razor
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")
@RenderSection("scripts", required: false)

Insert the js file(s) at the bottom of your razor view.

JavaScript
@section Scripts {
 <script type="text/javascript" src="@Url.Content("/Scripts/App/searchSelectionDialog.js")"></script>
 <script type="text/javascript" src="@Url.Content("/Scripts/App/index.js")"></script>
}

Insert “hidden” dialog div into your razor view. Notice the parameters of the dialog – data-search-url sets the function to call to search items and data-select-func sets the JavaScript function to be called after the user selects some item. To populate the dialog, I use bTest button.

Razor
<div id="testDlg" title="Demo Items"
  data-search-url="@Url.Action("SearchJSON", 
  "Home")" 
  data-select-func="itemSelected">Please wait</div>
<button id="bTest" 
class="btn btn-default">Let's select some item!</button>

Now you can initialize the dialog in JavaScript. I use separate js file (in my Visual Studio 2013, I was not able to debug JavaScript code within the razor view). There is also click handler for the bTest button.

JavaScript
$(document).ready(function () {
 initSearchSelectionDialog($('#testDlg'));
 $("#bTest").click(function () {
  $("#testDlg").dialog("open");
 });
});

Once the user populates the dialog, C# code is called using Ajax from your home controller. I didn’t want to put database into the demo program, so you can easily compile and test it. That’s why I created a sample list of items directly in this function.

C#
public JsonResult SearchJSON(string searchString)
{
 //create demo items
 var itemList = CreateDemoItems();

 var searchList = itemList;
 if (!string.IsNullOrEmpty(searchString))
 {
  searchList = itemList.FindAll(
   x => x.Name.ToUpper().Contains(searchString.ToUpper())).ToList();
 }

 //here you must convert your item attributes to Id, Name and Note
 //accepted by search dialog
 List<object> jsonList = new List<object>();
 foreach (var item in searchList)
 {
  jsonList.Add(new
  {
   Name = item.Name,
   Id = item.Id,
   Note = item.Note,
  });
 }

 return this.Json(jsonList, JsonRequestBehavior.AllowGet);
}

Points of Interest

To improve the code, you could add some paging option in the dialog and also probably it would be better to send only one page through Ajax/JSON call. But I don’t need that for my project.

History

  • 21st April, 2017: First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Showtacle s.r.o.
Slovakia Slovakia
Like to develop software for laser shows and LED performances.

Comments and Discussions

 
-- There are no messages in this forum --