Skip to content
Loading
Get Drop down List and bind the textbox values in MVC  
  •           
  •         class="col-xs-12 padd0">  
  •               
  •           
  •         class="col-xs-12 col-sm-6 col-sm-offset-3" style="margin-top:2%;">  
  •             class="form-horizontal">  
  •                 @Html.ValidationSummary(true""new { @class = "text-danger" })  
  •                 class="form-group">  
  •                     class="col-md-5" style="text-align:left">  
  •                         @Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "control-label" })  
  •                       
  •                     class="col-md-7">  
  •                         @Html.EditorFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" } })  
  •                         @Html.ValidationMessageFor(model => model.Id, ""new { @class = "text-danger" })  
  •                       
  •                   
  •   
  •                 class="form-group">  
  •                     class="col-md-5" style="text-align:left">  
  •                         @Html.LabelFor(model => model.ClientName, htmlAttributes: new { @class = "control-label" })  
  •                       
  •                     class="col-md-7">  
  •                         @Html.DropDownListFor(m => m.ClientName, (IEnumerable)ViewBag.ClientLists, "Select Client Name",  
  •                             new { @class = "form-control" })  
  •                         @Html.ValidationMessageFor(model => model.ClientName, ""new { @class = "text-danger" })  
  •                       
  •                   
  •   
  •                 class="form-group">  
  •                     class="col-md-5" style="text-align:left">  
  •                         @Html.LabelFor(model => model.ClientAddress, htmlAttributes: new { @class = "control-label" })  
  •                       
  •                     class="col-md-7">  
  •                         @Html.EditorFor(model => model.ClientAddress, new { htmlAttributes = new { @class = "form-control", autocomplete = "off" } })  
  •                         @Html.ValidationMessageFor(model => model.ClientAddress, ""new { @class = "text-danger" })  
  •                       
  •                   
  •   
  •                 class="form-group">  
  •                     class="col-md-offset-2 col-md-10 text-right">  
  •                         "submit" value="Submit" class="btn btn-primary" />  
  •                         "reset" value="Reset" class="btn btn-danger" />  
  •                       
  •                   
  •                 class="col-xs-12">  
  •                     @if (TempData["Message"] == "Success")  
  •                     {  
  •                         "color:green;"> Client Master Details Inserted Successfully.

      
  •                     }  
  •                     @if (TempData["Message"] == "Already")  
  •                     {  
  •                         "color:orangered;"> Client Master Details Already Exists.

      
  •                     }  
  •   
  •                   
  •   
  •               
  •           
  •     }  
  •     "text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">  
  •     "text/javascript">  
  •         $(function () {  
  •             // This will make every element with the class "date-picker" into a DatePicker element  
  •             $('.date-picker').datepicker();  
  •         })  
  •       
  •   
  •   
  • In My Controller part.
     
    1. public class ClientMasterController : Controller  
    2.     {  
    3.         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["PMSConn"].ConnectionString);  
    4.         // GET: ClientMaster  
    5.         public ActionResult Index()  
    6.         {  
    7.             return View(ShowClientMaster());  
    8.         }  
    9.   
    10.         public ActionResult getClientId()  
    11.         {  
    12.             ClientMaster objCliMas = new ClientMaster();  
    13.             DataSet ds = new DataSet();  
    14.             string sQuery = " SELECT ISNULL(MAX(ClientId), 0)+1 'MaxClientId' FROM PMS_ClientMaster ";  
    15.             SqlDataAdapter da = new SqlDataAdapter(sQuery, con);  
    16.             con.Open();  
    17.             da.Fill(ds);  
    18.             con.Close();  
    19.             if (ds.Tables[0].Rows.Count > 0)  
    20.             {  
    21.                 objCliMas.Id = Convert.ToInt32(ds.Tables[0].Rows[0]["MaxClientId"].ToString());  
    22.             }  
    23.   
    24.             return View(objCliMas);  
    25.         }  
    26.   
    27.         public List getClientList()  
    28.         {  
    29.             ClientMaster objCliMas = new ClientMaster();  
    30.             DataSet ds = new DataSet();  
    31.             string sQuery = "SELECT A.BranchCode,A.ClientId,A.ClntName,A.ClntAddress,B.ClntType FROM mstrClient AS A "  
    32.                         + " LEFT JOIN mstrClientName AS B ON A.ClientId = B.ClientId AND A.ClntName = B.ClntName AND A.BranchCode = B.BranchCode "  
    33.                         + " WHERE B.BranchCode = 'HSR-01' AND B.ClntType = 'Private' AND A.ClntStatus = 'Y' ORDER BY A.ClientId";  
    34.             SqlCommand cmd = new SqlCommand(sQuery, con);  
    35.             con.Open();  
    36.             SqlDataAdapter da = new SqlDataAdapter(cmd);  
    37.             da.Fill(ds);  
    38.             con.Close();  
    39.             List ClientLists = new List();  
    40.             for (int i = 0; i < ds.Tables[0].Rows.Count; i++)  
    41.             {  
    42.                 ClientMaster CMobj = new ClientMaster();  
    43.                 CMobj.ClientId = Convert.ToInt32(ds.Tables[0].Rows[i]["ClientId"].ToString());  
    44.                 CMobj.ClientName = ds.Tables[0].Rows[i]["ClntName"].ToString();  
    45.                 ClientLists.Add(CMobj);  
    46.             }  
    47.             return ClientLists;  
    48.         }  
    49.   
    50.         //public ActionResult getClientAddress(string sClientId)  
    51.         //{  
    52.         //    ClientMaster objClientList = new ClientMaster();  
    53.         //    DataSet ds = new DataSet();  
    54.         //    string sQuery = "SELECT A.BranchCode,A.ClientId,A.ClntName,A.ClntAddress,B.ClntType FROM mstrClient AS A "  
    55.         //                + " LEFT JOIN mstrClientName AS B ON A.ClientId = B.ClientId AND A.ClntName = B.ClntName AND A.BranchCode = B.BranchCode "  
    56.         //                + " WHERE B.BranchCode = 'HSR-01' AND B.ClntType = 'Private' AND A.ClntStatus = 'Y' AND B.ClientId = '" + sClientId.Trim() + "'";  
    57.         //    SqlDataAdapter da = new SqlDataAdapter(sQuery, con);  
    58.         //    con.Open();  
    59.         //    da.Fill(ds);  
    60.         //    con.Close();  
    61.         //    List ClientLists = new List();  
    62.         //    if (ds.Tables[0].Rows.Count > 0)  
    63.         //    {  
    64.         //        objClientList.ClientAddress = ds.Tables[0].Rows[0]["ClntAddress"].ToString();  
    65.         //        ClientLists.Add(objClientList);  
    66.         //    }  
    67.               
    68.         //    return View("Create", objClientList);  
    69.         //}  
    70.   
    71.         public ActionResult Create()  
    72.         {  
    73.             ClientMaster objCliMas = new ClientMaster();  
    74.             TempData["Message"] = "";  
    75.   
    76.             //var ClientMasterId = getClientId();  
    77.             //ViewBag.ClientIdLists = new SelectList(ClientMasterId, "Id");  
    78.             //return View(objCliMas);  
    79.   
    80.             var ClientMaster = getClientList();  
    81.             ViewBag.ClientLists = new SelectList(ClientMaster, "ClientName""ClientId", getClientId());  
    82.             return View(objCliMas);  
    83.         }  
    84.   
    85.         [HttpPost]  
    86.         public ActionResult Create(ClientMaster objCliMas)  
    87.         {  
    88.             try  
    89.             {  
    90.   
    91.                 var ClientMaster = getClientList();  
    92.                 ViewBag.ClientLists = new SelectList(ClientMaster, "ClientName""ClientId", getClientId());  
    93.   
    94.                 if (ModelState.IsValid)  
    95.                 {  
    96.                     string sReturn = string.Empty;  
    97.                     SqlCommand cmd = new SqlCommand("spPMS_ClientMaster", con);  
    98.                     cmd.CommandType = CommandType.StoredProcedure;  
    99.                     cmd.Parameters.AddWithValue("@QType""Insert");  
    100.                     cmd.Parameters.AddWithValue("@ClientId""");  
    101.   
    102.                     cmd.Parameters.AddWithValue("@ClientName", objCliMas.ClientName);  
    103.                     cmd.Parameters.AddWithValue("@ClientAddress", objCliMas.ClientAddress);  
    104.                     cmd.Parameters.AddWithValue("@cStatus""Y");  
    105.                     cmd.Parameters.AddWithValue("@BranchCode""HSR-01");  
    106.                     cmd.Parameters.AddWithValue("@CreatedBy""Admin");  
    107.                     SqlParameter SQLReturn = new SqlParameter("@SQLReturn", SqlDbType.NVarChar, 50);  
    108.                     SQLReturn.Direction = ParameterDirection.Output;  
    109.                     cmd.Parameters.Add(SQLReturn);  
    110.                     con.Open();  
    111.                     cmd.ExecuteNonQuery();  
    112.                     con.Close();  
    113.                     sReturn = SQLReturn.Value.ToString().Trim();  
    114.                     if (sReturn.Trim() == "Success")  
    115.                     {  
    116.                         TempData["Message"] = "Success";  
    117.                         ModelState.Clear();  
    118.                         //getproductList();  
    119.                         //return View(objCliMas);  
    120.                         return RedirectToAction("ShowClientMaster""ClientMaster");  
    121.   
    122.                     }  
    123.                     else if (sReturn.Trim() == "Already")  
    124.                     {  
    125.                         TempData["Message"] = "Already";  
    126.                         return View();  
    127.                     }  
    128.                     else  
    129.                     {  
    130.                         TempData["Message"] = "Some thing went wrong while User Created ";  
    131.                         return View();  
    132.                     }  
    133.                 }  
    134.                 else  
    135.                 {  
    136.                     return View();  
    137.                 }  
    138.             }  
    139.             catch (Exception ex)  
    140.             {  
    141.                 TempData["Message"] = ex;  
    142.                 return View();  
    143.             }  
    144.         }  
    145.   
    146.         public ActionResult ShowClientMaster()  
    147.         {  
    148.             List ShowClientmaster = new List();  
    149.             DataSet ds = new DataSet();  
    150.             string sQuery = "SELECT ClientId,ClientName,ClientAddress FROM PMS_ClientMaster WHERE cStatus='Y' AND BranchCode='HSR-01'";  
    151.   
    152.             SqlDataAdapter da = new SqlDataAdapter(sQuery, con);  
    153.             da.Fill(ds);  
    154.             if (ds.Tables[0].Rows.Count > 0)  
    155.             {  
    156.                 foreach (DataRow dr in ds.Tables[0].Rows)  
    157.                 {  
    158.                     ShowClientmaster.Add(new ClientMaster()  
    159.                     {  
    160.                         Id = int.Parse(dr[0].ToString()),  
    161.                         ClientName = dr[1].ToString(),  
    162.                         ClientAddress = dr[2].ToString(),  
    163.                     });  
    164.                 }  
    165.                 return View(ShowClientmaster);  
    166.             }  
    167.             else  
    168.             {  
    169.                 return RedirectToAction("Create");  
    170.             }  
    171.         }  
    172.   
    173.   
    174.         public ActionResult EditClientMaster(int sClientId)  
    175.         {  
    176.             try  
    177.             {  
    178.                 TempData["PRDId"] = sClientId;  
    179.                 ClientMaster objClient = new ClientMaster();  
    180.                 SqlDataAdapter da;  
    181.                 DataSet ds = new DataSet();  
    182.                 da = new SqlDataAdapter("SELECT * FROM PMS_ClientMaster WHERE ClientId = '" + sClientId + "'", con);  
    183.                 da.Fill(ds);  
    184.                 var ClientMaster = getClientList();  
    185.                 ViewBag.ClientLists = new SelectList(ClientMaster, "ClientName""ClientId");  
    186.                 for (int i = 0; i < ds.Tables[0].Rows.Count; i++)  
    187.                 {  
    188.                     objClient.ClientId = int.Parse(ds.Tables[0].Rows[i]["ClientId"].ToString());  
    189.                     //objClient.ProductId = int.Parse(ds.Tables[0].Rows[i]["ProductId"].ToString());  
    190.                     objClient.ClientName = ds.Tables[0].Rows[i]["ClientName"].ToString();  
    191.                     objClient.ClientAddress = ds.Tables[0].Rows[i]["ClientAddress"].ToString();  
    192.                 }  
    193.                 return View(objClient);  
    194.             }  
    195.             catch  
    196.             {  
    197.                 return View();  
    198.             }  
    199.         }  
    200.   
    201.         [HttpPost]  
    202.         public ActionResult EditClientMaster(ClientMaster objPRD)  
    203.         {  
    204.             try  
    205.             {  
    206.   
    207.                 var ClientMaster = getClientList();  
    208.                 ViewBag.ClientLists = new SelectList(ClientMaster, "ClientId""ClientName");  
    209.                 if (ModelState.IsValid)  
    210.                 {  
    211.                     string sReturn = string.Empty;  
    212.                     SqlCommand cmd = new SqlCommand("spPMS_ClientMaster", con);  
    213.                     cmd.CommandType = CommandType.StoredProcedure;  
    214.                     cmd.Parameters.AddWithValue("@QType""Update");  
    215.                     cmd.Parameters.AddWithValue("@ClientId", objPRD.ClientId);  
    216.                     cmd.Parameters.AddWithValue("@ClientName", objPRD.ClientName);  
    217.                     cmd.Parameters.AddWithValue("@ClientAddress", objPRD.ClientAddress);  
    218.                     cmd.Parameters.AddWithValue("@cStatus""Y");  
    219.                     cmd.Parameters.AddWithValue("@BranchCode""HSR-01");  
    220.                     cmd.Parameters.AddWithValue("@CreatedBy""Admin");  
    221.                     SqlParameter SQLReturn = new SqlParameter("@SQLReturn", SqlDbType.NVarChar, 50);  
    222.                     SQLReturn.Direction = ParameterDirection.Output;  
    223.                     cmd.Parameters.Add(SQLReturn);  
    224.                     con.Open();  
    225.                     cmd.ExecuteNonQuery();  
    226.                     con.Close();  
    227.                     sReturn = SQLReturn.Value.ToString().Trim();  
    228.                     if (sReturn.Trim() == "Success")  
    229.                     {  
    230.                         //TempData["Message"] = "Success";  
    231.                         ModelState.Clear();  
    232.                         return RedirectToAction("ShowClientMaster""ClientMaster");  
    233.                     }  
    234.                     else if (sReturn.Trim() == "Already")  
    235.                     {  
    236.                         TempData["Message"] = "Already";  
    237.                         return View();  
    238.                     }  
    239.                     else  
    240.                     {  
    241.                         TempData["Message"] = "Some thing went wrong while User Created ";  
    242.                         return View();  
    243.                     }  
    244.                 }  
    245.                 else  
    246.                 {  
    247.                     return View();  
    248.                 }  
    249.             }  
    250.             catch (Exception ex)  
    251.             {  
    252.                 TempData["Message"] = ex;  
    253.                 return View();  
    254.             }  
    255.         }  
    256.   
    257.   
    258.         public ActionResult Delete(int sClientId)  
    259.         {  
    260.             try  
    261.             {  
    262.                 //   mstrProduct objProduct = new mstrProduct();  
    263.                 string sQuery = "UPDATE PMS_ClientMaster SET cStatus = 'N' WHERE ClientId ='" + sClientId + "'";  
    264.                 SqlCommand cmd = new SqlCommand(sQuery, con);  
    265.                 con.Open();  
    266.                 cmd.ExecuteNonQuery();  
    267.                 con.Close();  
    268.                 return RedirectToAction("ShowClientMaster");  
    269.             }  
    270.             catch  
    271.             {  
    272.                 return View();  
    273.             }  
    274.   
    275.         }  
    276.   
    277.     }  
    So what are all changes I should do to my code, please let me know it. 
    0
  • Sreekanth Reddy
    Hi Sourav,
     
    Your View : 
    @Html.DropDownList("Sortby", new SelectListItem[]
    {
    new SelectListItem() { Text = "Newest to Oldest", Value = "0" },
    new SelectListItem() { Text = "Oldest to Newest", Value = "1" }},
    new { @onchange="BindTextBoxValue(this.value)"
    });
    Script in view : 
     
     
    Mark as answer, If it helps.
    Thank you. 
    +1
  • Sourav Kumar Das
     Hi,
     
    My question is while selecting one of the dropdown list value  at on selected change event the textbox should be binded. 
     
     
    Example:
    dropdownlist has all the client names while selecting one of the client name the textbox should return its client address based on the client name selected. 
    0
  • Faisal Pathan
    you want selected Dropdown value right? and with Jquery or somthing else ?
    +1