1

For my project, I would like to dynamically display a table containing information from my database. Unfortunately, I am unable to pass the c# array containing all of my database's information into a Javascript array. I have tried creating a hidden field like this:

<input type="hidden" id="tutors" name="tutors" value="<%: PeerTutoring.StaticData.GetTutorsSerialized()%>" />

The method GetTutorsSerialized is:

public static string GetTutorsSerialized()
    {
        char[] stuff = new JavaScriptSerializer().Serialize(new PeerTutoring.Models.PeerTutoringDataContext().Tutors).ToArray();
        return stuff.ToString();
    }

Next, I have tried accessing this information from Javascript like this:

var x = $('#tutors').val()
        alert(x);

This displays the message "System.Char[]" in the alert box. The length of the 'x' is also 13, which is the length of the string "System.Char[]".

Also, once I have got this array working, will I be able to access fields of the object's held in the Javascript array like this? :

x[0].Email

Thanks for the help.

4

2 回答 2

1

为什么不使用视图模型将数据传递给视图并使用 c# 生成表?

<table>
    <%
        foreach(var m in Model)
        { %>
            <tr>
                <td><%: m.Property1 %></td>
                <td><%: m.Property2 %></td>
            </tr>
        <% }
    %>
</table>

有关将数据传递到 MVC 中的视图的更多信息:http: //blogs.msdn.com/b/nunos/archive/2010/02/04/quick-tips-about-asp-net-mvc-how-do- i-pass-data-to-a-view.aspx

于 2013-06-14T01:39:37.530 回答
1

而不是.toString(),尝试new string(stuff);

.NET / C# - 将 char[] 转换为字符串

于 2013-06-14T02:08:58.760 回答