Solved

C# MVC ASP.net Dropdown List Change

Posted on 2016-08-01
4
74 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Data is not showing from images 15 38
Export import database 4 45
how to read json value 2 32
.NET tools for adding thread safety to a web app? 3 19
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

23 Experts available now in Live!

Get 1:1 Help Now