Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please Excuse Me Because my english language in not very well!
My Scenario is I want to get data from First dropdownliast and Bind to dynamic dropdownlist.

What I have tried:

In controller I do this for first dropdownlist:
C#
public ActionResult Create()
        {
            List<SelectListItem> Zone = new Karaneh_Inf().C_Tbl_Center.Select(p => new SelectListItem()
            {
                Text = p.ZoneName,
                Value = p.IdZone.ToString(),
            }).Distinct().ToList();
            ViewBag.Zone = Zone;
            return View();
        }


In View:
HTML
<div class="form-group">
            @Html.LabelFor(model => model.IdZone, "منطقه", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("zoneid", ViewBag.Zone as List<SelectListItem>, null, htmlAttributes: new { @class = "form-control", onChange = "Get(this.value)" })
                @Html.ValidationMessageFor(model => model.IdZone, "", new { @class = "text-danger" })
            </div>
        </div>

<select id="centers">
            <option>No-Data</option>
        </select>

Then I use jquery for ajax:
HTML
function Get(zoneid) {


            $.ajax('/Person/CenterDDL/' + zoneid).done(function (data) {
                $('#centers').append(data);
            });
        }

Also I write a action to create partialview for second dropdownliast:
C#
public PartialViewResult CenterDDL(int zoneid)
        {
            return PartialView(new Karaneh_Inf().C_Tbl_Center.Where(p => p.IdZone == zoneid).ToList());
        }

Then Create PartialView:
HTML
@using Karaneh.Models
@model List<c_tbl_center>


<div class="form-group">
    <label>Center</label>
    <div class="col-md-10">
        @foreach (C_Tbl_Center center in Model)
        {
            <option value="@center.CenterCode">@center.CenterName</option>
        }
    </div>
</div>


But when I run the project,doesn't created any dynamically dropdownliat!
Posted
Updated 4-Nov-16 23:18pm
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900