Friday, 26 July 2019

Repository Pattern Configuration using Unity Container

ASP.NET MVC with Unity Introduction
  1. Install the nuget package “Unity.Mvc”
  2. Make sure that the following classes have been added to your App_Start folder:
    • UnityConfig.cs
    • UnityMvcActivator.cs
  3. Navigate to the UnityConfig class, method RegisterTypes(IUnityContainer container) and start mapping your Interface/Class dependencies. For example, if you have an Email Service that some parts of your application will need to depend on, you will need to create an Interface IEmailService.cs with an implementation class EmailService.cs
    public static void RegisterTypes(IUnityContainer container) {
       // TODO: Register your type's mappings here.
       container.RegisterType<IEmailService, EmailService>();
    }
  4. Now that you have registered your mappings, you can then begin injecting these dependencies wherever they are needed. Examples below:
    public class HomeController : Controller {
       public readonly IEmailService _emailService;
       public HomeController(IEmailService emailService) {
          _emailService = emailService;
       }
       public ActionResult SendEmail(string title, string email, string message) {
          _emailService.Send(title, email, message);
          return null;
       }
    }
    Maybe on a separate controller too?
    public class FormsController : Controller {
       public readonly IEmailService _emailService;
       public FormsController(IEmailService emailService) {
          _emailService = emailService;
       }
       public ActionResult SendEmail(string title, string email, string message) {
          _emailService.Send(title, email, message);
          return null;
       }
    }
  5. OPTIONAL: If you have a Unit Test project, then you can pass on a different mapping to your interface like the below:
    container.RegisterType<IEmailService, TestEmailService>();
Example of Implementation

Step 1: Create Class Library Project for example Repository and add an interface IRepository as given below for generic operations implementation.

 public interface IRepository<T> where T : class
{
      T Find(Expression<Func<T, bool>> predicate);
      T GetById(Int64 id);
      T IQueryable<T> GetAll();
      T IQueryabl<T> Search(Expression<Func<T, bool>> predicate);
      bool IsExists(Expression<Func<T, bool>> predicate);
      int Insert(T entity);
      int Delete(T entity);
      int Update(T entity);
}

Step 2: Add Folders like Masters, Transactions etc in this project and put all other class related interface in these folders and inherit them with IRepository interface. You may put function definitions other than generics implementation above.

public interface ITaskAllocation : IRepository<Task_Allocation_Model)
{
    string ExcecuteTaskAllocation(Task_Allocation_Model obj);
}

Note : For every model add interface as above and models used in above class should in Models class library.

Step 3: In Helpers class library we will be putting all the Helpers containing implementations of Methods declared in interface. We will be using base class for implementations of Generic Methods implementation declared in Generic Interface IRepository above and inherit them with all other helpers classes.

For example we will putting TaskAllocationHelper class for ITaskAllocation Interface. 

 i) Put BaseClass in Common Folder in Helper Class Library. Base Class Generic Implementation is given below.

public class BaseClass<T> : IDisposable, IRepository<T> where T : class
{
    private readonly ApplicationDbContext _dbContext;
    public readonly int unit;
    public readonly string log;
    public BaseClass()
    {
        if(HttpContext.Current.User.Identity.IsAuthenticated)
        {
             if(_dbContext == null)
                 _dbContext = new ApplicationDbContext();

            if(unit == 0)
                unit = ApplicationSession.GetUserDetails().UM_CORP_UNIT;
            if(string.IsNullOrEmpty(log)
               log = CommonFunction.GetEntryLog(ApplicationSession.GetUserDetails().UM_UID);
        }

       public virtual T GetById(Int64 id)
       {
           _dbContext.Set<T>().Find(id);
       }
       public virtual IQueryable<T> GetAll()
       {
           _dbContext.Set<T>().AsQueryable();
       }
       public virtual IQueryable<T> Search(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
       {
           _dbContext.Set<T>().Where(predicate).AsQueryable();
       }
       public virtual bool IsExists(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
       {
           _dbContext.Set<T>().Any(predicate);
       }
       public int Insert(T entity)
      {
          _dbContext.Set<T>().Add(entity);
          _return _dbContext.SaveChanges();
      }
      public int Update(T entity)
      {
          _dbContext.Entry(entity).State = EntityState.Modified;
          _return _dbContext.SaveChanges();
      }   
      public int Delete(T entity)
      {
          _dbContext.Set<T>().Remove(entity);
          _return _dbContext.SaveChanges();
      }
      
      public void Dispose()
      {
           if(_dbContext != null)
           {
                _dbContext.Dispose();
                return;
           }
      }
    }


ii) Put Helper classes in specific Folders like Masters, Transactions in Helper Class Library.

public class TaskAllocationHelper : BaseClass<Task_Allocation_Model>, ITaskAllocation
{
       public string  ExcecuteTaskAllocation(Task_Allocation_Model obj)
      {
            string msg=string.Empty;
            try 
            {
                obj.Unit=base.Unit;
                if(obj.ActionType==(int)CrudType.Add)
                {
                   if(!base.IsExists(x=>x.Unit == obj.Unit)
                       msg=CommonFunction.GetOperationStatusMsg(base.Insert(obj),"AddSuccess");
                  else
                       msg=CommonFunction.GetOperationStatusMsg(-1,"AlreadyExists");
                }   
               else  if(obj.ActionType==(int)CrudType.Edit)
                {
                       msg=CommonFunction.GetOperationStatusMsg(base.Update(obj),                                                          "UpdateSuccess");
                }   
               else  if(obj.ActionType==(int)CrudType.Delete)
                {
                       obj.Status = 2; // Changing record status as deleted for soft delete operation. 
                       msg=CommonFunction.GetOperationStatusMsg(base.Update(obj), "Delete");
                }   
            }
            catch(Exception ex)
            {
                msg = CommonFunction(-2, "SystemError");
                return msg;
            }
      }
}

Step 4: Mapping in UnityConfig Registration in RegisterTypes Method in AppStart Folder.


public static void RegisterTypes(IUnityContainer container) {
   // TODO: Register your type's mappings here.
   container.RegisterTypes<ITaskAllocation, TaskAllocationHelper>();
   container.RegisterType<IEmailService, EmailService>();
}
Step 5: In Controller, Now that you have registered your mappings, you can then begin injecting these dependencies wherever they are needed as given below:

public class HomeController : Controller {
   public readonly ITaskAllocation _taskAllocation;
  public readonly IEmployee _emp;
public HomeController(ITaskAllocation taskAllocation, IEmployee emp) { this._emp=emp; this._taskAllocation = taskAllocation; } public ActionResult TaskAllocation() { return View(_taskAllocation.Search(x=>x.Status != CrudState.Deleted).ToList()); } [HttpPost] [ValidateAntiforgery] public JavaScriptResult TaskAllocation(Task_Allocation_Model obj) { if(ModelState.IsValid) return JavaScript(_taskAllocation.ExcecuteTaskAllocation(obj)); else return JavaScript(CommonFunction.GetOperationStatusMsg(1,CommonFunction.GetModelError(ViewData.ModelState)); } public PartialViewResult Search(int code, string desc) { var result =_taskAllocation.Search(x=>(code==0 ||x.Code==code) && (desc == null || x.Desc.ToLower().Contains(desc.ToLower())) && x.Status != CrudState.Deleted).ToList(); return PartialView("~/Views/Shared/Tasks/_TaskListPartial.cshtml", result); }

Step 6 : View TaskAllocation.cshtml

@model IEnumable<Models.Task_Allocation_Model>
@using Helpers.Common
@{
       Viewbag.Title="";
       Models.CommonModel obj=new Models.CommonModel();
}

<div class="">
<div>
       <input id="txtCode" type="number" oninput="javascript:if(this.value.length> this.maxLength)             this.value=this.value.Slice(0,this.maxLength); maxLength="5" />
        <input id="txtCode" type="text" maxLength="5" />
        <input id="btnSearch" type="button" value="Search" />
</div>
<div class="panel panel-primary filterable"  id="divListPartial">
        @{ Html.RenderPartial("~/Views/TaskListPartial.cshtml");  }
</div>
</div>
<div id="divPartial"></div>

@section scripts {

    <script type="text/javascript">

$( document ).ready(function() {

      $("#btnSearch").click(function (){
             
             var code = $("#txtCode").val();
             var desc=$("#txtDesc").val();

             if(code=="")
                 code=0;
             if(desc="")
                 desc=null;

             $.ajax({
                type: "POST",
                url: '/Home/Search',
                data: '{code: code, desc:desc }',
                success: function (result) {
                     $("#divListPartial").html(result);
                     $('table').DataTable();
                },
                error: function () {
                    alert("Error..");
                }

          });
});
     
    </script>

}

In Listing Partial View TaskListPartial.cshtml

<table class="table"> 
    <tr> 
        <th> 
            @Html.DisplayNameFor(model => model.TaskNo) 
        </th> 
        <th> 
            @Html.DisplayNameFor(model => model.TaskName) 
        </th> 
        <th> 
            @Html.DisplayNameFor(model => model.TaskDesc) 
        </th> 
        <th></th> 
    </tr> 
 
@foreach (var item in Model) { 
    <tr> 
        <td> 
            @Html.DisplayFor(modelItem => item.TaskNo) 
        </td> 
        <td> 
            @Html.DisplayFor(modelItem => item.TaskName) 
        </td> 
        <td> 
            @Html.DisplayFor(modelItem => item.TaskDesc) 
        </td> 
      @{ Html.RenderPartial("~/Views/Shared/_ListViewActionPartial.cshtml", Tuple.Create(item.ID,Common,LoadModal,'_TaskPartial.cshtml'));  }
    </tr> 

 

</table>

In _ListViewActionPartial.cshtml View : 

@model Tuple<int, string, string, string>
@{
       int edit=Models.Common.CommonModel.CrudType.Edit.GetHashCode();
       int delete=Models.Common.CommonModel.CrudType.Delete.GetHashCode();
       int view=Models.Common.CommonModel.CrudType.View.GetHashCode();
   }

<a class="glyphicon glyphicon-edit edit" title="Edit" onclick="showModal('@Model.Item1, @Model.Item2, @Model.Item3, @edit, @Model.Item4')">
<a class = "glyphicon glyphicon-delete delete" title = "Delete" onclick = "showModal('@Model.Item1, @Model.Item2, @Model.Item3, @delete, @Model.Item4')">
<a class="glyphicon glyphicon-view view" title="View" onclick="showModal('@Model.Item1, @Model.Item2, @Model.Item3, @view, @Model.Item4')">


In TaskPartial.cshtml : This partial view is used to add, edit or view Task.

In Common Controller : Action in Common Controller is used to return TaskPartial.cshtml.

[CustomAuthorize]
public class CommonController : Controller
{
   public readonly ITaskAllocation _taskAllocation;
   public readonly IEmployee _emp;
   public CommonController(ITaskAllocation taskAllocation, IEmployee emp)
   {
     this._emp=emp;
     this._taskAllocation = taskAllocation; 
    }

   [HttpPost] 
   public PartialViewResult LoadModal(CommonModel model)
   {
      dynamic obj=null;
      if(model.ActionType != CrudType.Add.GetHashcode())
      {
         if(model.partialViewName="_TaskPartial.cshtml")
             obj=_taskAllocation.Search(x=>x.ID == model.ID).FirstOrDefault();
         else if(model.partialViewName="_EmpPartial.cshtml")
             obj=_emp.Search(x=>x.ID == model.ID).FirstOrDefault();
       }
       else
       {
           if(model.partialViewName="_TaskPartial.cshtml")
              obj= new Task_Allocation_Model() { TaskId =                         CommonFunction.GetMaxTaskId("tblTaskAllocation","TaskId")};
else  if(model.partialViewName="_EmpPartial.cshtml")
              obj= new Emp_Model() { EmpId =                         CommonFunction.GetMaxTaskId("tblEmp","EmpId")};
       }

     if(obj!=null)
        obj.ActionType=model.ActionType;

    return PartialView("~/View/Shared/"+model.partialViewName, obj);

   }
 }

// todo :
Add Partial Views, common controller, scripts, custom authorize, exception logging

putting model and common function ex for more clarification, GetMaxTaskId function
ConvertToMsg renamed to getoperationstatusmsg

Friday, 4 January 2019

DropDownLists

DropDownLists

@{
    ViewBag.Title = "Home Page";
    var clientlist = (SelectList)ViewBag.ClientType;
    var listSub = Dictionary.MSP_GetClientList().ToList(); 
}
-----------------------------------------------------------------------------------------------------------------

@Html.DropDownListFor(m => m.ABC.Type, clientlist as SelectList, "Select Client Type", new { @id="ddlClient"})
-----------------------------------------------------------------------------------------------------------------
@Html.DropDownListFor(m => m.ABC.Type, new SelectList(string.Empty,"Value","Text"), "Select Client Type", new { @id="ddlClient"})
-----------------------------------------------------------------------------------------------------------------

     public static class ClsDictionary
        {
            public static Dictionary<int,string> MSP_GetClientList()
            {
                var list = new Dictionary<int,string>();
                list.Add(1,"MSP Client A");
                list.Add(2,"MSP Client B");
                return list;
            }
        }
-----------------------------------------------------------------------------------------------------------------
public ActionResult Index()
        {
            ViewBag.DefaultDate=DateTime.Now.ToString("dd-MMM-yyyy");
            ViewBag.ClientType = new SelectList(ClsDictionary.MSP_GetClientList().OrderBy(x=>x.Value).ToList(),"Key","Value");

            return View();
        }
--------------------------------------------------------------------------------------------------------------------

  public static List<MSP_Client> GetSubList()
        {
            return new List<MSP_Client>() {
                new MSP_Client() { subId=9, parentId=2, Name="MSP Supply"},
                new MSP_Client() { subId=10, parentId=2, Name="MSP Bill"},
                new MSP_Client() { subId=11, parentId=2, Name="MSP Order"},
                new MSP_Client() { subId=13, parentId=2, Name="MSP Product"}};
        }
----------------------------------------------------------------------------------------------------------------------

 public static List<SelectListItem> GetProductById(string _id)
        {
            var result = (from f in db.MSP_Product
                          join c in db.MSP_Customer
                          on f.ProductCode equals c.CustProdCode
                          where c.CustProdCode = _id
                          select new { f.ProductCode, f.ProductName }).Distinct();

            var list = result.select(x => new SelectListItem() { Value = x.ProductCode.ToString(), Text = x.ProductName.ToString() }).ToList();
            list.Insert(0, new SelectListItem() { Value = "0", Text = "Please Select" });
            return list;
        }
-----------------------------------------------------------------------------------------------------------------------
 
 [HttpGet]
        public JsonResult SaleAction(int? value)
        {
            var data = db.ItemModel.where(m => m.code == value).select(s => new SelectListItem()
            {
                Value = s.ItemCode.ToString(),
                Text = s.ItemName.ToString()
            }).ToList();

            return Json(new { data, JsonRequestBehavior.AllowGet });
        }

<Script>
    $(document).ready(function () {
        var counter = 0;
        var InitValue = 0;
        var _val = $("txtCode").val();
        $.getJSON('Url.Action("SaleAction","Home/SaleAction")' + "?value=" + _val, function (result) {
            $("#ddl").html();
            var data = result.data;
            $("#ddl").append("<option value = \"\">Select Item</option>");
            for(var i=0;i<data.length;i++)
            {
                $("#ddl").append("<option value=" + data[i].Value + ">" + data[i].Text + "</option>");
                if (counter == 0)
                    InitValue = data[i].Value;
                counter = counter + 1;
            }
        });
    });
  </Script>

Saturday, 29 December 2018

Creating helper popup in MVC5 to fill textbox itemname and description

Creating helper popup to select and fill item name and description on type item name on textbox as shown in figure below:






using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace msPoojaElectrical.Models.Material
{
    [Table("ItemMaster")]
    public class ItemMasterModel
    {
        public Int32 ITEM_APP_UNIT { get; set; }
        [Key]
        public Int64 ITEM_ID { get; set; }
        public string ITEM_CODE { get; set; }
        public string ITEM_NAME { get; set; }
        public string ITEM_DESC { get; set; }
        public string ITEM_RATE { get; set; }
        public string ITEM_QTY { get; set; }
        public string ITEM_STATUS { get; set; }

        public string ITEM_OPRLOG { get; set; }
        [NotMapped]
        public double ITEM_AMT { get; set; }
        [NotMapped]
        public string PopId { get; set; }
        [NotMapped]
        public string PopName { get; set; }
        [NotMapped]
        public List<SelectListItem> ddlItemType { get; set; }
    }
}

-----------------------------------------------------------------------------------

using msPoojaElectrical.Controllers;
using msPoojaElectrical.Models.Material;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace msPoojaElectrical.Areas.Material.Controllers
{
    public class ItemMasterController : BaseController
    {
        // GET: Material/ItemMaster
     
     
        public ActionResult CreateItem()
        {
         
            return View();
        }

        [HttpPost]
        public ActionResult CreateItem(ItemMasterModel itemMasterModel)
        {
         
            return View();
        }
     
    }

}

-----------------------------------------------------------------------------

@model msPoojaElectrical.Models.Material.ItemMasterModel

@{
    ViewBag.Title = "CreateItem";
    Layout = "~/Views/Shared/_LayoutNew.cshtml";
}

<style>
    #modalPopup {
        top: -425px;
        z-index: 99999999;
    }
</style>

<h2 class="text-center">Create Item</h2>
<br />
@using (Html.BeginForm("CreateItem", "ItemMaster", FormMethod.Post, new { @id = "formCreateItem" }))
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(model => model.PopId, new { htmlAttributes = new { @value = "txtItemCode", @id = "PopId" } })
    @Html.HiddenFor(model => model.PopName, new { htmlAttributes = new { @value = "txtItemDesc", @id = "PopName" } })
    <div class="form-horizontal">

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })


        <div class="form-group">
            @Html.LabelFor(model => model.ITEM_CODE, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-4">
                @Html.EditorFor(model => model.ITEM_CODE, new { htmlAttributes = new { @class = "form-control", @id = "txtItemCode", @onkeyup = "ShowItemCode();" } })
                @Html.ValidationMessageFor(model => model.ITEM_CODE, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ITEM_NAME, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-4">
                @Html.EditorFor(model => model.ITEM_NAME, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ITEM_NAME, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ITEM_DESC, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-4">
                @Html.EditorFor(model => model.ITEM_DESC, new { htmlAttributes = new { @class = "form-control", @id = "txtItemDesc" } })
                @Html.ValidationMessageFor(model => model.ITEM_DESC, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ITEM_RATE, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-4">
                @Html.EditorFor(model => model.ITEM_RATE, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ITEM_RATE, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.ITEM_QTY, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-4">
                @Html.EditorFor(model => model.ITEM_QTY, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ITEM_QTY, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.ITEM_AMT, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-4">
                @Html.EditorFor(model => model.ITEM_AMT, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ITEM_AMT, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="row">
            <div class="col-md-7 text-center">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </div>
    </div>
}

<div class="text-center">
    @Html.ActionLink("Back to List", "Index")
</div>

<script>
    $(document).ready(function () {
        $("#modalPopup").hide();
        $("#PopId").val('txtItemCode');
        $("#PopName").val('txtItemDesc');
    });

    function ShowItemCode() {

        debugger;
        var App_Unit = '@Request.RequestContext.HttpContext.Session["App_Unit"]';
        if (App_Unit == 0)
            App_Unit = 1;
        $.ajax({
            url: '@Url.Action("ItemHelper", "MasterHelp", new { area = ""})',
            data: {
                TableName: "ItemMaster",
                CodeField: "Item_Code",
                DescField: "Item_Desc",
                Condition: "Where Item_Code like '" + $("#txtItemCode").val().toUpperCase() + "%'"
            },
            type: "get"
        }).done(function (response) {
            $("#exampleModelLabel").html("");
            $("#exampleModelLabel").html("ItemCode");
            $("#modalBody").html(response);
            $("#modalPopup").show();
            $("#modalPopup").removeClass("fade");
        });
    };
</script>

---------------------------------------------------------------------------
In Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace msPoojaElectrical.Models
{
    public  class RawQuery
    {
        public class MasterHelp
        {
            public string Code { get; set; }
            public string Description { get; set; }
        }
    }
}


In MasterHelp Controller


using msPoojaElectrical.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using static msPoojaElectrical.Models.RawQuery;

namespace msPoojaElectrical.Controllers
{
    public class MasterHelpController : Controller
    {
        // GET: MasterHelp
        DataAccess db = new DataAccess();
        string strSql = "";
        public ActionResult ItemHelper(string TableName, string CodeField, string DescField, string Condition)
        {
            strSql = "Select " + CodeField + " as Code, " + DescField + " as Description from " + TableName + " "+ Condition;
            List<MasterHelp> data = db.Database.SqlQuery<MasterHelp>(strSql).ToList();
           // return PartialView("_ItemHelper",data);
            return PartialView("_Index",data);
        }
    }
}

---------------------------------------------------------------------------------
In Partial View _Index

@model List<msPoojaElectrical.Models.RawQuery.MasterHelp>

<script type="text/javascript">
    // todo ; uncomment
    //$(document.ready(function () {
    //    var Table = $('.HelpTableClass').DataTable({
    //        "ScrollY": "350px",
    //        "ScrollCollaspe": true,
    //        lengthChange: true
    //    });
    //}));

</script>
@*<script src="~/Content/Assets/js/jquery-2.1.4.min.js"></script>*@
@*<script src="~/Content/Assets/js/jquery-2.1.4.min.js"></script>*@
<style>
    /*TODO :*/
    /*Get Css for web grid*/
</style>


@{
    var grid = new WebGrid(source: Model, canPage: false, rowsPerPage: 20, ajaxUpdateContainerId: "gridContent");
}
<div id="gridContent">
    @grid.GetHtml(htmlAttributes: new { id = "HelpTable" },
                                        tableStyle: "HelpTableClass table table-striped table-bodered",
                                        headerStyle: "thead th-lg",
                                        columns: grid.Columns(grid.Column(columnName: "Code", header: "Code"),
                                                             grid.Column(columnName: "Description", header: "Description")
                                        ))
</div>

<script type="text/javascript">
    // todo ; uncomment
    //$(document).ready(function () {
    //    $("#gridContent a").removeAttr("href");
    //});

    $("#HelpTable").on('click', 'tbody > tr > td', function () {
     
        //var Id = $("#PopId").val();
        //var Name = $("#PopName").val();
        var Id = "txtItemCode";
        var Name = "txtItemDesc";

        //var Id2 = $("#ConId").val();
        //var Name2 = $("#ConName").val();
        var rowIndex = $(this).parent().html();
        //$("#modalPopup").modal('hide');
        $("#modalPopup").hide();
        $("#modalPopup").addClass("fade");

        var currentRow = $(this).closest("tr");
        var col1 = currentRow.find("td:eq(0)").html();
        var col2 = currentRow.find("td:eq(1)").html();
     
        $("#" + Id + "").val(col1);
        $("#" + Id + "").focus();
        if (Name != "") {
            $("#" + Name + "").val(col2);
            $("#" + Name + "").focus();
        }
     

        //$("#" + Id2 + "").val(col1);
        //$("#" + Id2 + "").focus();
        //if (Name2 != "") {
        //    $("#" + Name2 + "").val(col2);
        //    $("#" + Name2 + "").focus();
        //}

        $("#ConId").val('');
        $("#ConName").val('');
        $("#" + Id + "").trigger("change");
    });
</script>

-----------------------------------------------------------------------------------
In _LayoutNew

 <!---//End-da-slider----->


    <div id="modalPopup" class="modal-dialog fade" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-content">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
            <div class="modal-header">
                <h4 class="modal-title" id="exampleModelLabel"></h4>
            </div>
            <div class="modal-body" id="modalBody">
                @*<div class="wthree-info">
                 
                </div>*@
                <div class="row">
                    <div>
                        <div class="table-responsive"></div>
                    </div>
                </div>
                <div class="clearfix"></div>
            </div>
        </div>
    </div>