1

I have a .cshtml file which I am using the model:

@model IEnumerable<WebApplicationMVCTest.Models.Test>

The model contains the parameter Date (among other parameters). I access this value when creating a table:

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.SiteID)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Date)
                </td>
        ...

I have a function in my code NotTested and I want to pass the item.Date value into the function as one of the arguments:

<script>
         document.onload = NotTested("Site 001", item.Date);

         function NotTested(SiteId, TestDate) { ... }
</script>

The problem is two-fold. 1) I know that the Date is part of the model in this view so that it is present onload when the function is called, but 2) the model is out of context in the <script> section of code.

I know that item is not present onload since this is is only transient for the for loop. I have just used item.Date as a placeholder argument.

How do I get access to this when calling the function?

Ala
  • 1,505
  • 1
  • 20
  • 36
tomdertech
  • 497
  • 1
  • 9
  • 27
  • 1
    Are you wanting to call this function for each item in your model? –  Jun 21 '15 at 12:39
  • No. Just the Date item and only when the form loads – tomdertech Jun 21 '15 at 12:46
  • I meant that you have a loop - so did you want to pass the `Date` property for each item in the collection? –  Jun 21 '15 at 12:49
  • If so there are a number a ways you could do this - e.g. assign the model to a javascript variable = refer [this answer](http://stackoverflow.com/questions/30730055/jquery-how-to-traverse-iterate-over-a-list-of-object/30730172#30730172), but it would be easiest just to add a class name (say) `@Html.DisplayFor(modelItem => item.Date)` element and use `$('.date').each(function() { NotTested("Site 001", $(this).text()) });` –  Jun 21 '15 at 12:54
  • The `Date` item is actually the same because of model I am loading in. It is the "other" parameters which change in when looping through the table. That is why I can call this `onload` and know that the `Date` is the same. – tomdertech Jun 21 '15 at 13:05
  • If the date does not vary, then you could assign it to a `ViewBag` property and use `var date = JSON.parse('@Html.Raw(Json.Encode(ViewBag.Date))'); NotTested("Site 001", date);` Note also you should wrap the script in [$.document.ready()](https://learn.jquery.com/using-jquery-core/document-ready/) - not using `document.onload` –  Jun 21 '15 at 13:11
  • @StephenMuecke - you strike again! Thank you this works perfectly :-) If you want to write this as an answer I will accept it. – tomdertech Jun 21 '15 at 13:19

2 Answers2

1

From your comments, property Date is the same for each item in your collection, so you can assign it to a javascript variable using

var date = JSON.parse('@Html.Raw(Json.Encode(Model[0].Date))');

or in the controller, assign in to ViewBag.Date = yourDate; and in the script

var date = JSON.parse('@Html.Raw(Json.Encode(ViewBag.Date))');

You can then use call the function using

<script>
  $(function() {
    var date = JSON.parse('@Html.Raw(Json.Encode(ViewBag.Date))');
    NotTested("Site 001", date);
  });
</script>
0

Assign a css class to the date item and use jquery to select the date items based on the css class as following:

@Html.DisplayFor(modelItem => item.SiteID, new { htmlAttributes = new { @class = "mySiteIDClass" } })

@Html.DisplayFor(modelItem => item.Date, new { htmlAttributes = new { @class = "myDateClass" } })

<script>
         document.onload = NotTested($(".mySiteIDClass"), $(".myDateClass"));

         function NotTested(SiteIds, TestDates) { //loop through date items }
</script>
Ala
  • 1,505
  • 1
  • 20
  • 36