使用json调用控制器方法 [英] Calling controller method using json
本文介绍了使用json调用控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
美好的一天,我想要一个< select>标记 要加载的视图< options>通过json查看加载。
这是我的模型类
public class 类别
{
公开列表< string> ID = new 列表< string>();
public 列表< string> Name = new List< string>();
public 列表< string> lgaID = new List< string>();
public 列表< string> lgaName = new List< string>();
}
此 我想要被称为来自 json
public ActionResult getLGA()
{
尝试
{
类别c = 新类别();
char [] chars = {' ,'};
testStealthServ.countries csd = new testStealthServ.countries();
List< string> sth = csd.getlocal( 2)。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;
}
}
这个方法通过给我一个数组列表按预期工作。我认为问题在于javascript / jquery调用
我希望我的控制器方法填充< select>在下面的视图中标记但是不起作用
< select id = schllga class = form-control-select required> < / select >
< script language = javascript>
$ .get( getLGA, null ,BindData);
function BindData(custs)
{
var sel = $ ( #schllga);
for ( var j = 0 ; j< custs.length; j ++)
{
var newOpt = < option> + custs [j] .lgaName + < /选项> 中跨度>;
sel.append(newOpt);
}
}
< / script>
我也试过这个但它没有'也工作
< select id = schllga class = form-control-选择 required> < / 选择 >
< script>
$ .get( @ Url.Action( getLGA ), function (费用){
< span class =code-keyword> var $ sel = $( #schllga);
$ sel.empty();
for ( var j = 0 ; j< custs.length; j ++){
$( < option />)
.text(custs [j] .lgaName)
.appendTo($ sel);
}
});
< / script>
请指导我的错误所在。谢谢
解决方案
.get( getLGA, null ,BindData);
function BindData(custs)
{
var sel =
( #schllga);
for ( var j = 0 ; j< custs.length; j ++)
{
var newOpt = < option> + custs [j] .lgaName + < /选项> 中跨度>;
sel.append(newOpt);
}
}
< / script>
我也试过这个但它没有'也工作
< select id = schllga class = form-control-选择 required> < / 选择 >
< script>
.get( @ Url.Action( getLGA ), function (cost){
var < /跨度>
good day, i want a <select> tag in a view to be loaded with <options> upon view load via json.
this is my model class
public class Category
{
public List<string> ID = new List<string>();
public List<string> Name = new List<string>();
public List<string> lgaID = new List<string>();
public List<string> lgaName = new List<string>();
}
this is my controller class method that i want to be called from json
public ActionResult getLGA()
{
try
{
Category c = new Category();
char[] chars = { ',' };
testStealthServ.countries csd = new testStealthServ.countries();
List<string> sth = csd.getlocal("2").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;
}
}
this method works as expected by giving me an array list. I think the issue is with the javascript/jquery call
I want my controller method to populate a <select> tag in the view below but that isn't working
<select id="schllga" class="form-control-select" required></select>
<script language="javascript">
$.get("getLGA", null, BindData);
function BindData(custs)
{
var sel = $("#schllga");
for (var j = 0; j < custs.length; j++)
{
var newOpt = "<option>" + custs[j].lgaName + "</option>";
sel.append(newOpt);
}
}
</script>
I also tried this bt it doesn't work too
<select id="schllga" class="form-control-select" required></select>
<script>
$.get("@Url.Action("getLGA")", function(costs) {
var $sel = $("#schllga");
$sel.empty();
for (var j = 0; j < custs.length; j++) {
$("<option/>")
.text(custs[j].lgaName)
.appendTo($sel);
}
});
</script>
pls guide me as to where my mistake(s) are. Thanks
解决方案
.get("getLGA", null, BindData); function BindData(custs) { var sel =
("#schllga"); for (var j = 0; j < custs.length; j++) { var newOpt = "<option>" + custs[j].lgaName + "</option>"; sel.append(newOpt); } } </script>
I also tried this bt it doesn't work too
<select id="schllga" class="form-control-select" required></select>
<script>
.get("@Url.Action("getLGA")", function(costs) { var
这篇关于使用json调用控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文