You must Sign In to post a response.
  • Category: ASP.Net MVC

    How fill dropdownlist on radiobutton selection in MVC 4

    HI,
    I am trying to fill dropdownlist on radiobutton selection. But I am not getting any value indropdonlist..

    code :
    public ActionResult GetProjectName(int ProjectID)
    {
    int incVal;
    DataTable source;

    VehicleEntity vehicleEntity;
    VehicleLogic vehicleLogic;

    try
    {
    vehicleEntity = new VehicleEntity();
    vehicleLogic = new VehicleLogic();

    vehicleEntity.SectionID = Convert.ToInt32(ProjectID); ;

    source = new DataTable();
    source = vehicleLogic.GetProjectNameData(vehicleEntity);

    List<VehicleEntity> vehicleList = new List<VehicleEntity>();

    for (incVal = 0; incVal < source.Rows.Count; incVal++)
    {
    vehicleEntity.ProjectID = Convert.ToInt32(source.Rows[incVal]["ProjectID"].ToString());
    vehicleEntity.ProjectName = source.Rows[incVal]["ProjectName"].ToString();
    vehicleList.Add(vehicleEntity);
    }

    //ViewBag.ProjectID = new SelectList(vehicleList, "ProjectID", "PROJECT_NAME", ProjectID);

    //return View(ViewBag.ProjectID);

    return Json(vehicleList, JsonRequestBehavior.AllowGet);

    }
    catch (Exception ex)
    {
    throw ex;
    }
    }

    View Code :
    =======
    <tr>
    <td style="color:maroon; font-weight:bold;">Contractor :</td>
    <td style="color:maroon; font-weight:bold;">
    <div class="col-md-7">
    @Html.RadioButton("IRB", "1", new { id = "departmentList" }) IRB
    </div>

    <td colspan="2"></td>
    </tr>
    <tr>
    <td style="color:maroon; font-weight:bold;">Project Name :</td>
    <td>
    <div class="col-md-12">
    @Html.DropDownListFor(Model => Model.ProjectID, new SelectList(Enumerable.Empty<SelectListItem>(), "ProjectID", "PROJECT_NAME"),
    new { @class = "form-control", id = "ddlProject" })
    </div>
    </td>
    <td style="color:maroon; font-weight:bold;">Name of Toll Plaza :</td>
    @*<td>@Html.DropDownList("ddlTollName", null, new { @class = "form-control" })</td>*@
    </tr>

    javascript :
    ======
    @section Scripts {
    <script>
    function FillCity() {
    var ContractorID = $('#departmentList').val();
    $.ajax({
    url: '/DatewiseCountReport/GetProjectName',
    type: "GET",
    dataType: "JSON",
    data: { Project: ContractorID },
    success: function (vehicleList) {
    $("#ddlProject").html(""); // clear before appending new list
    $.each(vehicleList, function (i, Project) {
    $("#ddlProject").append(
    $('<option></option>').val(vehicleList.ProjectID).html(vehicleList.ProjectName));
    });
    }
    });
    }
    </script>
    }
  • #769389
    Hi Patil,
    please try this code. This code is working fine for me.
    $('input[type=radio]').on('change', function () {
    var request = $.ajax({
    url: '@Url.Action("GetProjectName", "DatewiseCountReport")',
    method: "POST",
    data: { "ProjectID": ContractorID}
    });
    request.done(function (data) {
    var items = "";
    $.each(data, function (i, resdata) {
    items += "<option value='" + resdata.Value + "'>" + resdata.Text + "</option>";
    });
    $('#ddlProject').html(items);
    });
    });

  • #769394
    Good job..


  • Sign In to post your comments