开发者

How to preserve a C# list in my view page, to manipulate that in client side

I m using ASP.Net MVC 3.0. My model has a list of telephone. I want to preserve that in some way, so that I can manipulate & display later using Jquery in my开发者_JAVA技巧 script.

I tried saving the list in a hidden field, but I am not able to retrieve that in my JavaScript.

Please help me finding a way out. Thx in advance.


You could create a property on your viewmodel which holds a json serialized version of the phonenumbers like this:

public class Person
    {
        public string Firstname { get; set; }
        public string Lastname { get; set; }
        public IList<string> PhoneNumbers { get; set; }
        public IHtmlString SerializedPhoneNumbers
        {
            get { return MvcHtmlString.Create(new JavaScriptSerializer().Serialize(PhoneNumbers)); }
        }
    }

You can use that in the view like:

<script type="text/javascript">
    var phoneNumbers = @Model.SerializedPhoneNumbers 
</script>

You now can manipulate the collection in your clientside script.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜