如何将类对象作为参数添加到@ url.Action [英] How to add a class object as parameter to @url.Action

查看:107
本文介绍了如何将类对象作为参数添加到@ url.Action的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我在下拉列表的onchange事件中调用javascript函数如下



 function poplgas() {
$ .getJSON(' @ Url.Action(getLGA,Ministry)',function(costs){
var $ sel = $( #schLga);
$ sel.empty();
$ .each(成本,函数(索引,元素){
$( < option />)。text(element).appendTo($ sel);
});
});
}





此控制器中的Action有一个模型类对象作为参数

< pre lang =c#> public ActionResult getLGA(类别c)
{
尝试
{
getMethods.getState(c);

char [] chars = {' ,'};


int statepos = c.Name.IndexOf(c.statename); // 这将获得状态在列表中的位置
string stateindex = c.ID.ElementAt(statepos).ToString(); // 这将获取州ID的值。

testStealthServ.countries csd = new testStealthServ.countries();
List< string> sth = csd.getlocal(stateindex).ToList< string>();
foreach var v in sth)
{
string [] splits = v.Split(chars);

c.lgaName.Add(splits.ElementAt( 0 ));
c.lgaID.Add(splits.ElementAt( 1 ));
}

return Json(c.lgaName,JsonRequestBehavior.AllowGet);
}
catch (例外情况)
{
throw ex.InnerException;
// var mess = ex.Message +\ n+ ex.StackTrace;
// Response.Write(< script> alert(mess);< / script> );
}
}



我的javascript代码无效,因为我需要传递一个包含参数的对象一条路线。在这种情况下,这是类对象类别c;



我该怎么做。

感谢您的帮助。

解决方案

.getJSON(' @ Url.Action(getLGA ,部门)',功能(成本){
var


sel =


#schLga);


Hi all, i'm calling a javascript function in an onchange event of a dropdownlist as follows

function poplgas() {
                                $.getJSON('@Url.Action("getLGA", "Ministry")', function (costs) {
                                    var $sel = $("#schLga");
                                    $sel.empty();
                                    $.each(costs, function (index, element) {
                                        $("<option/>").text(element).appendTo($sel);
                                    });
                                });
                            }



this Action in the controller has a model class object as its parameter

public ActionResult getLGA(Category c)
        {
            try
            {
                getMethods.getState(c);
                
                char[] chars = { ',' };

                
                int statepos = c.Name.IndexOf(c.statename); //this gets the position where the state is in the list
                string stateindex = c.ID.ElementAt(statepos).ToString();  //this gets the value of the state ID.

                testStealthServ.countries csd = new testStealthServ.countries();
                List<string> sth = csd.getlocal(stateindex).ToList<string>();
                foreach (var v in sth)
                {
                    string[] splits = v.Split(chars);

                    c.lgaName.Add(splits.ElementAt(0));
                    c.lgaID.Add(splits.ElementAt(1));
                }

                return Json(c.lgaName, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
                //var mess = ex.Message + "\n" + ex.StackTrace;
                //Response.Write("<script>alert(mess);</script>");
            }
        }


my javascript code isn't working because i need to pass An object that contains the parameters for a route. in this case which is the class object Category c;

please how do i do that.
Thanks for your assistance.

解决方案

.getJSON('@Url.Action("getLGA", "Ministry")', function (costs) { var


sel =


("#schLga");


这篇关于如何将类对象作为参数添加到@ url.Action的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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