Avatar of ukerandi
ukerandi
Flag for United Kingdom of Great Britain and Northern Ireland asked on

C# MVC ASP.net Dropdown List Change

hi,
I have write a code to when dropdownList change event, according to the dropdown number i need to change the records in the same page.

See below code
View
       <label>   @Html.DropDownList("LeadsID", (SelectList)ViewBag.Values, new { onchange="FindLeadDetails(this.value)" })</label>

 <label>Firs tname</label>
       @Html.TextBoxFor(fname => fname.Firstname)
       <label>Last Name</label>
       @Html.TextBoxFor(lname => lname.Lastname)
       <label>Comapny name</label>
       @Html.TextBoxFor(cname => cname.Companyname)
       <label>Address </label>
       @Html.TextBoxFor(model => model.Address1)

Open in new window


Model.cs

 private string connstring = ConfigurationManager.ConnectionStrings["UsersContext"].ConnectionString;
        public string LeadsOwner {get;set;}
        public string Title {get;set;}
        public string Firstname {get;set;}
        public string Lastname {get;set;}
        public string Companyname {get;set;}
        public string Address1 {get;set;}
        public string Address2 {get;set;}
        public string Address3 {get;set;}
        public string Address4 {get;set;}
        public string City {get;set;}
        public string TelephoneNumber  {get;set;}
        public string Webaddress {get;set;}
        public string LeadStatus  {get;set;}
        public string LeadsSource  {get;set;}
        public string Industry  {get;set;}
        public DateTime CreatedDate { get; set; }
        public string Email { get; set; }
        public SelectList LeadID { get; set; }
        public List<SelectList> LeadsID { get; set; }
        public IEnumerable<SelectListItem> Items { get; set; } 
        public void FindLeadsDetailsById(string LeadID)
        {
            string custTitle = string.Empty;
            SqlConnection con = new SqlConnection(connstring);
            con.Open();
            SqlCommand Leadscommand = con.CreateCommand();
            Leadscommand.CommandText = "SELECT LeadsID,Title,Firstname,Lastname,Companyname,Address1,TelephoneNumber,Webaddress,LeadStatus,LeadsSource,Industry FROM tblLeads WHERE LeadsID='"+LeadID+"'";
            SqlDataReader rdrReadr = Leadscommand.ExecuteReader();
            while (rdrReadr.Read())
            {

                Firstname = rdrReadr["Firstname"].ToString();
            
            
            }
            rdrReadr.Close();
            Leadscommand.Dispose();
            con.Close();
            
            //tblLeads

           
        }

        public List<SelectListItem> pupulateLeadsID()
        {
            string strLeadID = string.Empty;
            SqlConnection con = new SqlConnection(connstring);
             List<SelectListItem> itemsID = new List<SelectListItem>();
            con.Open();
            SqlCommand cmdLeadsID = con.CreateCommand();
            cmdLeadsID.CommandText = "SELECT LeadsID FROM tblLeads";
            SqlDataReader rdrLeadID = cmdLeadsID.ExecuteReader();
            while (rdrLeadID.Read())
            {
                itemsID.Add(new SelectListItem { Text = rdrLeadID[0].ToString(), Value = rdrLeadID[0].ToString() });
            
            }
            rdrLeadID.Close();
            cmdLeadsID.Dispose();
            con.Close();

            return itemsID;
        
        }

Open in new window


Controler

  public ActionResult UpdateLeads()
            {

                Leads ldl = new Leads();
                ViewBag.LeadsID = ldl.pupulateLeadsID();


                return View();
            }

            public ActionResult FindLeadDetails(string LeadID)
            {

                Leads ld = new Leads();
                ld.FindLeadsDetailsById(LeadID);



                return View(ld);
            }

Open in new window

C#ASP.NET.NET Programming

Avatar of undefined
Last Comment
ukerandi

8/22/2022 - Mon
Prakash Samariya

Wild guess, you could do such like:

//keep exisiting below code as it is
public ActionResult UpdateLeads() //your orginal Action UI
{ .... }

Open in new window


//New action method
public ActionResult UpdateLeads(Leads ld) //new Action UI
{
	//below line Changed
	Leads ldl =  (ld!=null)? ld: new Leads();
	ViewBag.LeadsID = ldl.pupulateLeadsID();

	//below line Changed
	return View(ldl);
}

//Modified existing action method
public ActionResult FindLeadDetails(string LeadID)
{
	Leads ld = new Leads();
	ld.FindLeadsDetailsById(LeadID);

	//below line Changed
	return View("UpdateLeads",ld);
}

Open in new window

ukerandi

ASKER
Thanks How to change View
<label>   @Html.DropDownList("LeadsID", (SelectList)ViewBag.Values, new { onchange="FindLeadDetails(this.value)" })</label>

This part not working
ASKER CERTIFIED SOLUTION
Prakash Samariya

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ukerandi

ASKER
thx
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes