Lamloumi Afif
Lamloumi Afif

Reputation: 9081

Writing JavaScript script inside C# code

I have an asp.net application and i need to combine C# and javascript code:

<td >
    <label name="tag@(i)">@Model[1][i]._tag</label>
</td>
<td>
    <input type="text" value="@Model[1][i]._client" name="client@(i)"/>
</td>
<td>
    <input type="text" value="@Model[1][i]._reception"  class="datepicker" name="reception@(i)"/>
</td>
<td>
    <input type="text" value="@Model[1][i]._cloture"  class="datepicker" name="cloture@(i)"/>
</td>
<td>
    @{
        List<Planning.Models.Impaire> liste = u.Get_Impaire_List();
        Planning.Models.Impaire imp = liste.Find(x => x.id_paire == Model[1][i].Id);

        @{
            string reception =  @: <script>
            @: var f1 = $('input[name="reception' + @i.ToString() + '"]').val();
            @: document.write(f1);
            @: </script>
            ;
            string cloture =  @: <script>
            @: var f2 = $('input[name="cloture' + @i.ToString() + '"]').val();
            @: document.write(f2);
            @: </script>
            ;
            string client =  @: <script>
            @: var f3=  $('input[name="client' + @i.ToString() + '"]').val();
            @: document.write(f3);
            @: </script>
            ;
            string tag =  @: <script>
            @:var f4= $('input[name="tag' + @i.ToString() + '"]').val();
            @: document.write(f4);
            @: </script>
            ;
        }

        @Html.ActionLink("enregistrer","Index", new {identificateur = Model[1][i].Id, Pages = "1", _reception =reception, _cloture = cloture, _client = client, _tag = tag  })
    </td>  

But I have an exception in this line : @: var f1 = $('input[name="reception' + @i.ToString() + '"]').val(); error

What are the reasons of this error? How can I fix it?

Upvotes: 0

Views: 1548

Answers (1)

citykid
citykid

Reputation: 11070

Having js inside the view like your sample is highly unlucky and can always be avoided. Find a way to separate javascript from razor code as good as you can. You could leave javascript variables inside the view alike

var arr = [@i.ToString()]

and then, in a separate js file you process this js variable arr.

Upvotes: 1

Related Questions