Showing posts with label ASP.NET MVC. Show all posts
Showing posts with label ASP.NET MVC. Show all posts

Friday, November 16, 2012

Add AutoComplete feature to Textbox in ASP.NET MVC using JQuery UI

Explore the Top Microsoft SQLServer Technical/ Interview Questions here: http://XploreSqlServer.blogspot.com/
   
Explore the Top Microsoft C# Technical/ Interview Questions here: http://XploreCSharpDotNet.blogspot.com

Explore the Top Microsoft Blazor Technical/ Interview Questions here: https://XploreBlazor.blogspot.com/


1. Add a reference to JQuery javascript file and jQuery UI javascript file in the Razor view or in the _Layout.cshtml :


<
script src="jquery-1.8.2.js" type="text/javascript"></script>



<script src="ui/1.9.0/jquery-ui.js" type="text/javascript"></script>


Download the recent version of jQuery and jQuery UI javascript file from here:

jQuery:
http://jquery.com/download/
jQuery UI: http://jqueryui.com/download/

2. Add the following script block in the Razor View:



<
script type="text/javascript">

        $(function () {
          $("#txtCountry").autocomplete({
                source: '@Url.Action("SearchByCaption", "Country")'
         });
                 
        });

</
script>

The above script adds autocomplete feature to a textbox called txtCountry.
As the user types the characters in the textbox,  SearchByCaption method of CountryController is called through jQuery UI scripts. The typed string is passed as the parameter to the method.

3. Implement SearchByCaption action method in CountryController as follows:
public ActionResult SearchByCaption(string term)
{
       CountryBiz countryBiz;
       List<string> list;

       countryBiz = new CountryBiz(); 
       list = countryBiz.SearchByCaption(term);

       return Json(list, JsonRequestBehavior.AllowGet);
}
Important: the parameter of the action method should be named 'term'.

The SearchByCaption method of CountryBiz should perform the pattern search in the Country database table based on the parameter, 'term'.



Explore the Top Microsoft SQLServer Technical/ Interview Questions here: http://XploreSqlServer.blogspot.com/

Explore the Top Microsoft C# Technical/ Interview Questions here: http://XploreCSharpDotNet.blogspot.com

Tuesday, October 23, 2012

Create Custom Display Template for Boolean Datatype in ASP.NET MVC


Explore the Top Microsoft SQLServer Technical/ Interview Questions here: http://Xplore2WinSqlServer.blogspot.com/
   
Explore the Top Microsoft C# Technical/ Interview Questions here: http://XploreCSharpDotNet.blogspot.com

Explore the Top Microsoft Blazor Technical/ Interview Questions here: https://XploreBlazor.blogspot.com/

The ASP.NET MVC's @Html.DisplayFor method by default displays a disabled checkbox for boolean values.
Inorder to display the text, 'Yes' or 'No' instead of a checkbox, create the custom display template for boolean types as follows:

1. Create a folder called DisplayTemplates under the folder,  ~/Views/Shared.
2. Create a file called Boolean.cshtml to have the following code:

@model System.Boolean?

@if (Model.HasValue)
{

      if (Model.Value)
      {
         <text> Yes </text>
      }

      else
      {
         <text> No </text>
      }
}

else
{
      <text> &nbsp; </text>

}


Explore the Top Microsoft SQLServer Technical/ Interview Questions here: http://Xplore2WinSqlServer.blogspot.com/

Explore the Top Microsoft C# Technical/ Interview Questions here: http://XploreCSharpDotNet.blogspot.com