在View JQuery中获取ViewData

时间:2015-06-25 12:00:55

标签: c# jquery asp.net-mvc asp.net-mvc-4 telerik

我有一个通用列表,我将它作为ViewData从我的Controller传递到我的.aspx视图。 我需要使用Jquery脚本来迭代它。

我怎样才能使这个脚本有效?

此致

success: function (result) {

 var myArray = new Array();
 var myArray = '<%: ViewData["List"] %>';

 for (var i = 0; i < myArray.length; i++) {
 alert(i);
 }

5 个答案:

答案 0 :(得分:1)

尝试这样,将列表视图的项目推送到javascript数组,将下面的代码包装在脚本标记

--script tag--
var jList = new Array();
@foreach (var item in ViewData["List"] as List<String>)
   {
       @:jList.push('@item'); // That will work if its a one letter string but It doesnt know that its a string untill you use ''
 }
--script tag--

答案 1 :(得分:1)

你可以这样做......

<script type="text/javascript">
@{ List<string> ss = (List<string>)ViewData["List"];}
    var myArray = [
    @for (int i = 0; i < ss.Count; i++)
    {
        @: @(ss[i]),
    }
    ]
</script>

我的剃刀视图语法(你可以为经典做法)

答案 2 :(得分:1)

好的,如果您只使用字符串列表,那么

这样做

$(document).ready(function () {
@{List<string> listFromController = (List<string>)ViewData["List"];}
    var myArray = [
    @for (int i = 0; i < listFromController.Count; i++)
    {
        @: '@(listFromController[i])',
    }
    ]
});

但是如果您传递的是另一种类型的列表而不是像学生员工或用户那样的字符串,则需要以下

  

请使用您通过的相应课程和   属性假设“UserName”可以是“FirstName”“EmpId”或者什么

$(document).ready(function () {

        @{ var listFromController = (List<KnockoutJSWebApi.Models.LoginViewModel>)ViewData["list"];}
        var totalArray = [];
            @for (int i = 0; i < listFromController.Count; i++)
            {

                <text>
        var thisArray= {
            'username': '@(listFromController[i].UserName)',
            'password': '@(listFromController[i].Password)'
        };
        totalArray.push(thisArray);
            </text>
            }
 });

Aspx View Engine语法:

 <script>
        $(document).ready(function () {
            <% List<string> listFromController = (List<string>)ViewData["List"]; %>
            var myArray = [
             <% for (int i = 0; i < listFromController.Count; i++){ %>

                '<%: listFromController[i] %>',
              <% } %>
            ]
            debugger;
});
</script>

答案 3 :(得分:1)

Problem solved. I used a JsonResult method in my Controller and returned the needed values as:

Json(List, JsonRequestBehavior.AllowGet); 

With that, I don't need to iterate the values because I assign them as datasource to my DropDownList control.

$("#Generic_FK").data("DropDownList").dataSource.data(result);

Hope to help other people with the same issue!

答案 4 :(得分:0)

服务器端

  public class users
        {        
            public string name{ get; set; }
        }

        // in your controller code
        ViewData["list"] = new List<users>(new users[] { new users() 
                          { name="prasad"}, new users() {name="raja"}});

客户端

<script type="text/javascript">
$(function(){
    var myitems=JSON.parse('@Html.Raw(Json.Encode(this.ViewData["list"]))');

    $.each(myitems, function(index, item) {
      var name = item.name;
    });
});
</script>

我希望这可以帮到你