如何在asp.net mvc2应用程序的jquery方法中访问ViewModel属性

如何在asp.net mvc2应用程序的jquery方法中访问ViewModel属性,jquery,asp.net-mvc-2,Jquery,Asp.net Mvc 2,我正在开发asp.net mvc2应用程序。我的ViewModel定义如下: public class TestViewModel { /// <summary> /// Get and Set the EnabledSections /// </summary> public List<string> EnabledSections { get; set; } } 我需

我正在开发asp.net mvc2应用程序。我的ViewModel定义如下:

    public class TestViewModel
    {
        /// <summary>
        /// Get and Set the EnabledSections
        /// </summary>
        public List<string> EnabledSections { get; set; }
    }
我需要访问jquery方法中的EnabledSections:

function CheckEnabledSection(){
}

有谁能指导我解决上述问题吗?

首先,您必须将视图模型传递到视图中,以便在您的操作中看起来像:

return View("ViewName",model);
此外,请确保在视图中声明模型的类型:

@model TestViewModel
然后从视图中,需要将模型中的任何信息添加到js中:

<script>
  @{
    var jsSerializer = new JavaScriptSerializer();
  }
  var enabledSections = @jsSerializer.Serialize(Model.EnabledSections);//serialize the list into js array

</script>

@{
var jsSerializer=新的JavaScriptSerializer();
}
var enabledSections=@jsSerializer.Serialize(Model.enabledSections)//将列表序列化为js数组
现在,您可以在javascript中访问js变量
enabledSections

<script>
  @{
    var jsSerializer = new JavaScriptSerializer();
  }
  var enabledSections = @jsSerializer.Serialize(Model.EnabledSections);//serialize the list into js array

</script>