将arraylist从MVC razor视图传递给javascript [英] passing arraylist from MVC razor View to javascript

查看:63
本文介绍了将arraylist从MVC razor视图传递给javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC视图中有列表,我需要将其传递给javascript函数,有人可以帮助我!!!



谢谢

I have list in MVC view and i need to pass this to javascript function, can someone help me out !!!

Thanks

推荐答案

写这个



in Java Script Write this

function GetList()
{
var siteRoute=


#hdsiteroute)。val(); // 在cshtml或aspx中有一个隐藏字段,其id和值为Url.Content(〜),这将有助于实时模式
var url = siteRoute + YourController / GetList
("#hdsiteroute").val() ;// in cshtml or aspx have a hidden field with this id and value as Url.Content("~") this will help in live mode var url=siteRoute+"YourController/GetList"


.ajax({url: url,data:{yourargument: 1 },输入: GET,dataType: json async false , cache: false ,错误:ajaxError,成功:函数(结果){
for (< span class =code-keyword> var i = 0 ; i < result.length; i ++){
alert(结果[ 0 ]); // 访问这样的每个项目。如果列表是任何复杂类型,则使用结果[0] .Name //假设name是复杂类型的proprty
}

}});

}
函数ajaxError(jqXHR,textStatus,errorThrown){
// CATCH ERROR HERE
}
.ajax({ url: url, data: {yourargument:1}, type: "GET", dataType: "json", async: false, cache: false, error: ajaxError, success: function (result) { for (var i = 0; i < result.length; i++) { alert(result[0]); // access each item like this. if list is of any complex type the use result[0].Name // say name is a proprty of the complex type } } }); } function ajaxError(jqXHR, textStatus, errorThrown) { // CATCH ERROR HERE }





并且在控制器中有





and in controller have

public ActionResult GetList(int yourargument)
       {
           List<int> MyList = new List<int>(); //LIST CAN BE OF ANY COMPLEX DATATYPE
           MyList.Add(1);
           return Json(MyList, JsonRequestBehavior.AllowGet);
       }


这篇关于将arraylist从MVC razor视图传递给javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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