2013-10-07 68 views
0

我有一个asp.net应用程序,我需要结合C#和JavaScript代码:内的C#代码编写JavaScript脚本

<td > 
    <label name="[email protected](i)">@Model[1][i]._tag</label> 
</td> 
<td> 
    <input type="text" value="@Model[1][i]._client" name="[email protected](i)"/> 
</td> 
<td> 
    <input type="text" value="@Model[1][i]._reception" class="datepicker" name="[email protected](i)"/> 
</td> 
<td> 
    <input type="text" value="@Model[1][i]._cloture" class="datepicker" name="[email protected](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> 

但我在这行例外:@: var f1 = $('input[name="reception' + @i.ToString() + '"]').val();error

是什么这个错误的原因?我该如何解决它?

+0

你有什么建议吗? –

回答

1

让js在视图中像你的样本一样非常不吉利,总是可以避免的。找到一种尽可能好地将JavaScript与剃须刀代码分开的方法。你可以离开的JavaScript变量视图一样

var arr = [@i.ToString()] 

,然后里面,在单独的js文件您处理这个js变量ARR。

+0

我在添加'var arr = [@ i.ToString()]' –

+1

时得到了相同的结果,图片中的错误信息是不可读的,因为它太小了,法语第二,我不太好在。看起来你的模板代码是无效的。也许会剪掉部分,直到它找到它挂起的位置。 – citykid

+0

'编译错误 描述:编译满足此需求所需的资源时发生错误。请参阅下面有关错误的详细信息,并适当修改您的源代码。 编译器错误消息:CS1002 :;预计' –