Tuesday, January 14, 2014

How to use jQuery within @foreach inside Razor view


In this topic, lets review how to use jQuery in a Razor view within a foreach to loop through model data items and handle their properties.

To achieve that, our first line will be to implement the model that will contain data to be displayed.

    @model IEnumerable<DemoNamespace.Models.TestData>


Once we have the model defined, we can loop through the model and use jQuery to enable / disable a property. This will be handy, when you have dynamically generated id values based on EmpID.

      @if(Model != null)
      {
            foreach (DemoNamespace.Models.TestData items in Model.ToList())
                {
                    @:$('#' + items.EmpID).prop("disabled", false);
                }
        }


Happy coding....

No comments:

Post a Comment