在JavaScript中增加Razor变量是不行的

时间:2014-03-20 18:17:52

标签: c# javascript jquery asp.net-mvc razor

我尝试过一个分配页面,如果你想添加额外的人,你必须点击“再添加一个”按钮。我有一个计数变量,第一次当我点击按钮并调用该函数时,事情运行良好,但是当我第二次点击按钮时,我意识到之前的计数增量没有得到反映。我应该如何使增量永久或任何其他解决方案,我可以实现这一点。

@{
    int count = 1;
}
<script>
function AddNewDoc(ElementId) {
//This Increment is not reflected when the function is called again
@{
count++;
}

document.getElementById(ElementId).innerHTML = "<td>A</td>" + "<td>B</td>" + "    <td>C</td>" + "<td><input type=\"button\" value=\"Add One More Doctor\"      onclick=\"AddNewDoc(@count)\" /></td></tr>" + "<tr id=\"@count\">";
 }
</script>
@Html.ActionLink("GO BACK TO OPD POSTING", "UnitAndMonth", "OPD")
    <br />

    <br />
    <h2>@ViewBag.msg</h2>

    @using (Html.BeginForm("HandleForm", "OPD", FormMethod.Post))
    {  <table>
    <tr>
        <td>
            @Html.TextBoxFor(x => x[count].dr_name, new { @class = "input1", @id = "tags" })
        </td>
        <td>
            @Html.TextBoxFor(x => x[count].from_date, new { @class = "input1", @id = "datepicker" + count, @readonly = "readonly" })
        </td>

        <td>
            @Html.TextBoxFor(x => x[count].to_date, new { @class = "input1", @id = "datepicker" + count + count, @readonly = "readonly" })
        </td>
        <td>
            <input type="button" value="Add One More Doctor" onclick="AddNewDoc(@count)" />
        </td>

    </tr>
      <tr id="@count"></tr>

    </table>
        <input type="submit" value="Submit" />
    }

1 个答案:

答案 0 :(得分:0)

我认为在上面的代码中你的数量总是2。你也应该重新考虑

    <tr id="count"></tr>

因为id应该比描述更具描述性。

尝试摆脱所有@count,例如:

    <script>

    var count = 1;
    function AddNewDoc(ElementId) {
            count = count+1;
            document.getElementById(ElementId).innerHTML = "<td>A</td>" + "<td>B</td>" + "    <td>C</td>" + "<td><input         type=\"button\" value=\"Add One More Doctor\"      onclick=\"AddNewDoc(count)\" /></td></tr>" + "<tr id=\"count\">";
     }

     </script>

     <input type="button" value="Add One More Doctor" onclick="AddNewDoc(count)" />