我有一个用户数组,我想作为一个
javascript数组传递给一个视图.我是通过
JavaScriptSerializer这样做的,但我得到的字符串中没有未转义的引号.
控制器动作
public virtual ActionResult Create(int id)
{
var viewmodel = new Attendeeviewmodel(); var data = viewmodel.GetMembershipUserList() ; viewmodel.MeetingID = id; viewmodel.Users = new JavaScriptSerializer().Serialize(data.ToArray()); return View(viewmodel); }
视图
<script type="text/javascript"> var tags = @Model.Users $(document).ready(function () { $("#mytags").tagit({ availableTags:tags}); }); </script>
视图模型
public class Attendeeviewmodel { public int AttendeeId { get; set; } public string Name { get; set; } public int MeetingID { get; set; } public string Users { get; set; } public List<string> GetMembershipUserList() { List<string> userNames = new List<string>(); foreach (MembershipUser user in Membership.GetAllUsers()) { userNames.Add(user.UserName); } return userNames; } }
这就是Model.Users输出的原因
["DennisLeary","BobSaget","BillHicks"]
如何使用JavaScriptSerializer或任何其他方法转义引号?