• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 9972
  • Last Modified:

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

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
finance_teacher
Asked:
finance_teacher
  • 2
  • 2
1 Solution
 
StephanLead Software EngineerCommented:
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
 
finance_teacherAuthor Commented:
Do you have an example website showing this ?
0
 
finance_teacherAuthor Commented:
Please reply.
0
 
StephanLead Software EngineerCommented:
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

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

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