如何使用ajax从razor调用动作控制器方法 [英] how to call action controller method from razor using ajax

查看:57
本文介绍了如何使用ajax从razor调用动作控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨亲爱的,



我必须在dropdownlist onchange事件上调用动作控制器方法并将值加载到另一个下拉列表,所以我使用了ajax但是没有工作。我的代码在下面



控制器方法

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

[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

@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或javascript



Ajax or 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;
            }

推荐答案

(#hidURL)。val());
var sel = document .getElementById(' drpCate')。value;
alert(sel);
尝试 {

// var newDataURl = dataURL.replace('data:image / png; base64,','');
("#hidURL").val()); var sel = document.getElementById('drpCate').value; alert(sel); try { //var newDataURl=dataURL.replace('data:image/png;base64,', '');


.ajax({
< span class =code-comment> // url:/ AnswerSheetValuation / Main / WriteCoordiatesNew,
url:urlMap,
类型: POST
contentType:' application / json; charset = utf-8'
data: JSON .stringify({' catid':sel}),
成功:< span class =code-keyword> function (mydata){
alert(' Success');
}
});

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

// 返回dataURL;
}
.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; }


一切看起来都很好,建议请检查你的网址。网址应为/ {Controller} / {Action} /。
Everything looks fine, suggestion is please check your url. Url should be /{Controller}/{Action}/.


这篇关于如何使用ajax从razor调用动作控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆