0

我正在研究 asp.net mvc2 应用程序。我有一个 ViewModel 定义如下:

    public class TestViewModel
    {
        /// <summary>
        /// Get and Set the EnabledSections
        /// </summary>
        public List<string> EnabledSections { get; set; }
    }

我正在使用操作方法中的列表填充 TestViewModel 的属性 EnabledSections :

public ActionResult TestAction(Student student, string[] sections = null)
{
      var model = new TestViewModel 
      {
        EnabledSections  = model.TestSections.Where(s => s.Value.Item2 == true).Select(s => s.Key).ToList();
      }
}

我需要在 jquery 方法中访问 EnabledSections :

function CheckEnabledSection(){
}

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

4

1 回答 1

2

Firstly, you must pass your view model into your view, so from within your action it would look something like:

return View("ViewName",model);

Also, be sure to declare the type of model within your view:

@model TestViewModel

Then from within your view, you need to add any information from the model into your js:

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

</script>

Now you can access the js variable enabledSections within your javascript

于 2013-04-23T20:04:49.840 回答