Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# MVC ASP.net Dropdown List Change

Posted on 2016-08-01
4
Medium Priority
?
140 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 10

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 10

Accepted Solution

by:
Prakash Samariya earned 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

636 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