[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 146
  • Last Modified:

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

0
ukerandi
Asked:
ukerandi
  • 2
  • 2
1 Solution
 
Prakash SamariyaIT ProfessionalCommented:
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

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

This part not working
0
 
Prakash SamariyaIT ProfessionalCommented:
You need to reset Viewbag like below:
//Modified existing action method
public ActionResult FindLeadDetails(string LeadID)
{
	Leads ld = new Leads();
	ld.FindLeadsDetailsById(LeadID);

	ViewBag.LeadsID = ld.pupulateLeadsID();

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

Open in new window

Try with code your original code
<label>   @Html.DropDownList("LeadsID", (SelectList)ViewBag.Values, new { onchange="FindLeadDetails(this.value)" })</label>

or try with new one if not working
<label>   @Html.DropDownList("LeadsID", (SelectList)ViewBag.LeadsID, new { onchange="FindLeadDetails(this.value)" })</label>

Open in new window

0
 
ukerandiAuthor Commented:
thx
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now