Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want bind data in drowpdownlist when i choose value from another dropdownlist.

This is My Code:-

//--------Model-------//
// DeviceProduct Model //
C#
[Table("DeviceProduct")]
    public partial class DeviceProduct
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public DeviceProduct()
        {
            this.DeviceInfoe = new HashSet<DeviceInfo>();
            this.DeviceModel = new HashSet<DeviceModel>();
        }
        public int ID { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<DeviceInfo> DeviceInfoe { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<DeviceModel> DeviceModel { get; set; }
    }


// DeviceModel Model //
C#
[Table("DeviceModel")]
public partial class DeviceModel
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public DeviceModel()
    {
        this.DeviceInfoe = new HashSet<DeviceInfo>();
    }
    public int ID { get; set; }
    public Nullable<int> DeviceProductID { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<DeviceInfo> DeviceInfoe { get; set; }
    public virtual DeviceProduct DeviceProduct { get; set; }
}


// in Controller //
C#
public ActionResult NewDeviceInfo()
       {
           ViewBag.DeviceProductList = db.DeviceProduct;
           return PartialView("NewDeviceInfo");
       }

       [HttpPost]
       [ValidateAntiForgeryToken]
       public ActionResult NewDeviceInfo(DeviceInfo deviceInfo)
       {
           return view();
       }

       public ActionResult FillDeviceModel(int productId)
       {
           var deviceModels = db.DeviceModel.Where(c => c.DeviceProductID ==  productId);
           return Json(deviceModels, JsonRequestBehavior.AllowGet);
       }


// in view //
HTML
<div class="form-group col-lg-6">
                            @Html.LabelFor(m => m.DeviceProduct.Name, new { @class = "col-lg-2 control-label" })
                            <div class="col-lg-8">
                                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                                @Html.DropDownListFor(m => m.DeviceProduct, new SelectList(ViewBag.DeviceProductList, "ID", "Name"), WhiteWhaleLanguage.PleaseSelect, new { @class = "form-control" , @onchange="FillDeviceModel()" })
                                @Html.ValidationMessage("UserErrorMsg", htmlAttributes: new { @class = "text-danger" })
                            </div>
                        </div>
                        <div class="form-group col-lg-5">
                            @Html.LabelFor(m => m.DeviceModel.Name, new { @class = "col-lg-2 control-label" })
                            <div class="col-lg-9">
                                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                                @Html.DropDownListFor(m => m.DeviceModel, new SelectList(Enumerable.Empty<SelectListItem>(), "ID", "Name"), WhiteWhaleLanguage.PleaseSelect, new { @class = "form-control" })
                                @Html.ValidationMessage("UserErrorMsg", htmlAttributes: new { @class = "text-danger" })
                            </div>
                        </div>


// script code //

function FillDeviceModel()
{
var deviceproductid = $('#DeviceProduct').val();
$.ajax({
url: '/ClientsNotifications/FillDeviceModel',
type: "GET",
datatype: "JSON",
data: { DeviceProductID: deviceproductid },
success: function (deviceModels) {
$("#DeviceModel").html("");
$.each(deviceModels, function (i, devicemodel) {
$("#DeviceModel").append(
$('<option></option>').val(DeviceModel.ID).html(DeviceModel.Name));
})
},
error: function()
{
alert("Whooaaa! Something went wrong..")
}
});
}


What I have tried:

i write this code but it's do't work, where's the problem?
Posted
Comments
F-ES Sitecore 20-Mar-17 10:18am    
Use the debugging tools to step through your code to see what is\isn't happening. Does the FillDeviceModel js method get called? Does deviceproductid have the value you expect? Does the FillDeviceModel action get called?

https://forums.asp.net/t/1982579.aspx?Using+the+browser+s+dev+tools+to+diagnose+ajax+problems+and+other+things+

One thing I can see just by glancing at the code is that the FillDeviceModel action expects a param called productId but you are passing DeviceProductID. The two names have to match so .net knows which parameter gets which value.

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