Click here to Skip to main content
15,896,730 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Dear,

i have to call action controller method on dropdownlist onchange event and load value to another dropdownlist so i used ajax but not working. my code below

Controller method
C#
[HttpPost]
        public JsonResult WriteCoordiatesNew(int catid)
        {
            try
            {
                List<SubCategory> sublstcontent = new List<SubCategory>();
                {
                    //Get all page content from TblHome table
                    sublstcontent = DatabaseAccess.SubCategory(catid);

                };

                List<SelectListItem> subcatlist = new List<SelectListItem>();
                //List<string> items = new List<string>();
                foreach (var item in sublstcontent)
                {
                    subcatlist.Add(new SelectListItem
                    {
                        Text = item.sub_cate,
                        Value = item.sub_cat_id.ToString()
                    });
                }

                return Json(subcatlist, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ee)
            {
                Console.WriteLine(ee.Message);

                //WriteLog("Exception in Saving Canvas " + txtLocation + " " + ee.Message);
            }

            return null;
        }


In View
SQL
@Html.Label("Select Category")
                    @Html.DropDownList("category_name", (IEnumerable<SelectListItem>)ViewBag.categorylist, "--Select--", new { id = "drpCate", onChange = "dropdownsub()" })
                    <br />
                    @Html.Label("Select Sub-Category")
                    @Html.DropDownList("subcat_name", (IEnumerable<SelectListItem>)ViewBag.SubCatLst, "--Select--", new { id = "drpSubCate" })


Ajax or javascript
JavaScript
function dropdownsub() {

                var urlMap = document.getElementById('SubCat').value;
                //alert("URL: "+$("#hidURL").val());                
                var sel = document.getElementById('drpCate').value;
                alert(sel);
                try {

                    //var newDataURl=dataURL.replace('data:image/png;base64,', '');

                    $.ajax({
                        //url: "/AnswerSheetValuation/Main/WriteCoordiatesNew",
                        url: urlMap,
                        type: "POST",
                        contentType: 'application/json; charset=utf-8',
                        data: JSON.stringify({ 'catid': sel }),
                        success: function (mydata) {
                            alert('Success');
                        }
                    });

                }
                catch (e) {
                    alert(e);
                }

                //return dataURL;
            }
Posted
Comments
Ishpreet Kaur 23-Apr-14 3:19am    
Suppose, controller class is FirstController.cs

and your method in controller class is WriteCoordiatesNew(int catid)


then In Javascript,
write url

$.ajax({
url: "/First/WriteCoordiatesNew",
type: "POST",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ 'catid': sel }),
success: function (mydata) {
alert('Success');
}
});

1 solution

Everything looks fine, suggestion is please check your url. Url should be /{Controller}/{Action}/.
 
Share this answer
 

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