Solved

C# MVC ASP.net Dropdown List Change

Posted on 2016-08-01
4
66 Views
Last Modified: 2016-08-19
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
Comment
Question by:ukerandi
  • 2
  • 2
4 Comments
 
LVL 8

Expert Comment

by:Prakash Samariya
ID: 41738782
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
 
LVL 10

Author Comment

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

This part not working
0
 
LVL 8

Accepted Solution

by:
Prakash Samariya earned 500 total points
ID: 41740434
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
 
LVL 10

Author Closing Comment

by:ukerandi
ID: 41762110
thx
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now