Solved

ASP.Net MVC 4 C# -- DropDown List Selected Index Change EVENT ?

Posted on 2013-05-17
4
8,701 Views
Last Modified: 2013-05-31
Does anyone have other MVC examples for the below besides
http://asp-net-samples.blogspot.com/2013/04/dropdown-list-selected-index-change.html ?

Steps
 1. user makes a selection (i.e. "AW - Spec 123")
    from webpage's "Category" dropdown
 2. textbox1 and textbox2 automatically populate
    with details from MS SQL database, erasing previous text
 3. user manually changes textbox1 and textbox2 if needed
0
Comment
Question by:finance_teacher
  • 2
  • 2
4 Comments
 
LVL 16

Expert Comment

by:Stephan
ID: 39176986
Another approach is adding a attribute to the dropdownlist like so:

 @Html.DropDownListFor(model => model.Subbutton_Id, new SelectList(ViewBag.Subbutton, "Id", "series_name"), "-- Select --", new { onchange = "changeTextboxes" })

Open in new window


And add some javascript code:
<script type="text/javascript">
  function changeTextboxes(event) { 
       var selectBox = document.getElementById("Subbutton_Id");
       var tb1 = document.getElementById("textbox1");
       var tb2 = document.getElementById("textbox2");
       if (selectBox.value == "someValue") {
                 tb1.value = "Some value 1";
                 tb2.value = "Some value 2";
       }
  }
</script>

Open in new window


This is just an example. if you have something set you would like to implement it this way, please post the code and we'll make the modifications in order to get it to work.
0
 

Author Comment

by:finance_teacher
ID: 39192056
Do you have an example website showing this ?
0
 

Author Comment

by:finance_teacher
ID: 39200056
Please reply.
0
 
LVL 16

Accepted Solution

by:
Stephan earned 500 total points
ID: 39200343
Sorry for the late response.

I have a very simple version of this using jquery Ajax to set the values.

This is the model:
public class HomeModel
    {
        public string Category { get; set; }
    }

Open in new window


This is the view
@model MvcApplication4.Models.HomeModel
@{

    ViewBag.Title = "Index";
}

<h2>Index</h2>


@Html.DropDownListFor(model => model.Category, ((string[])ViewBag.Categories).Select(x => new SelectListItem { Text = x, Value = x }), new { onchange = "changeTextboxes()" })
<input type="text" id="tb1" />
<input type="text" id="tb2" />
<script type="text/javascript">
    function changeTextboxes() {
        var category = document.getElementById("Category");
        $.post("/Home/GetValues", { category: category.value }, function (data) {
            document.getElementById("tb1").value = data.tb1;
            document.getElementById("tb2").value = data.tb2;
        });
    }
</script>

Open in new window


And this is the controller:
public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            ViewBag.Categories = new string[] { "AW - Spec 123", "AC - Spec 456", "AC - Spec 789" };
            return View();
        }

        public JsonResult GetValues(string category)
        {
            return Json(new { tb1 = category + "tb1", tb2 = category + "tb2" });
        }
    }

Open in new window

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

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 …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

14 Experts available now in Live!

Get 1:1 Help Now