Pages

Thursday, 21 November 2019

Auto suggestion MVC C# ASP.Net

Auto Suggestion of names using C# MVC ASP.Net with Ajax

CSS



<style>
     .g-auto {
         position: absolute;       
        
         border: solid 1px #e3e2de;
         background-color: #ffffff;
         -webkit-box-shadow: 0px 3px 13px -1px rgba(0,0,0,0.75);
         -moz-box-shadow: 0px 3px 13px -1px rgba(0,0,0,0.75);
         box-shadow: 0px 3px 13px -1px rgba(0,0,0,0.75);
         padding:0px;
         display:none;
        
     }

       
         .g-auto select{height:200px;width:300px}
             .g-auto select option:hover {
                 background-color: #34495e;
                 color:#ffffff;
             }
 </style>

HTML

Karigar :<input type="text" id="txtKarigar"  onkeyup="GetNames()" class="form-control" />
<div class="g-auto " id="divSearchData">
    


Java Script


function GetNames()
{
    $j("#divSearchData").show();
    $j.ajax({
        url: '/User/GetNames/',
        dataType: "json",
        type: "GET",
        contentType: 'application/json; charset=utf-8',
        data: {name : $j("#txtKarigar").val()},
        success: function (data) {
            if (data != '') {
                $("#ddlNames").empty();
                var html = "";
                $j.each(data, function (i, item) {
                    html += "<option value='" + item.ID + "' onclick='fillText()'>" + item.Name + "</option>";




                })
                $j("#ddlNames").append(html);
            }
        }
    });
}


Controller



//Get Name search
public JsonResult GetNames(string name)
{
    List<KarigarModel> data = new List<KarigarModel>();
    UserRepository ob = new UserRepository();
    data = ob.GetNames(name);
    return Json(data, JsonRequestBehavior.AllowGet);

}


Repository



public List<KarigarModel> GetNames(string name)
{
    List<KarigarModel> Record = new List<KarigarModel>();
    try
    {
        using (SqlConnection dbcon = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnection"].ToString()))
        {
            using (SqlCommand cmd = new SqlCommand("[dbo].[GetSugeestion]", dbcon))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@arg", name);

                if (dbcon.State == ConnectionState.Closed)
                    dbcon.Open();
                using (SqlDataReader drOb = cmd.ExecuteReader())
                {
                    if (drOb.HasRows)
                    {
                        while (drOb.Read())
                        {
                            KarigarModel data = new KarigarModel();
                            data.ID = drOb.IsDBNull(0) ? " " : Convert.ToString(drOb[0]);
                            data.Name = drOb.IsDBNull(1) ? " " : Convert.ToString(drOb[1]);
                            Record.Add(data);
                        }
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {

    }
    return Record;
}


Store Procedure



USE [rajkumar9795_benarasfabric]
GO
/****** Object:  StoredProcedure [dbo].[GetAllKarigar]    Script Date: 21-Nov-19 5:58:15 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/* get  all karigar for entry of image */
create Proc[dbo].[GetAllKarigarForEntry]

AS

BEGIN
       select ID, Name,    Address
FROM            Karigar order by Name

END

No comments:

Post a Comment