如何在txt文件CSHTML中增加值

时间:2019-02-13 04:20:56

标签: javascript c# html

我正在创建一个.txt文件输出“ data.txt”。在文件内部,我想在用户每次选择特定按钮时设置一个增量值。 例如,如果用户单击“投票约翰”。 第一次,“约翰,1岁” 第二次,“约翰2岁”

但是,我的价值本身并没有增加。请指教。谢谢。

@{
    var result = "";
    int countJohn = 1;

    if (IsPost)
    {
        if (Request["submit"] == "Vote Harry")
        {

            //var harry = Request["Harry"];

            int countHarry = 1;

            var userData = "Harry" + "," + countHarry + Environment.NewLine;

            var dataFile = Server.MapPath(@"~/App_Data/data.txt");

            File.AppendAllText(@dataFile, userData);

           // countHarry += countHarry;  does not increases the count inside  .txt at all

            result = "Information saved.";
        }

        else if (Request["submit"] == "Vote John")
        {

            //var john = Request["John"];

            var userData = "John" + "," + (Int32.Parse("1")) + Environment.NewLine;

            var dataFile = Server.MapPath(@"~/App_Data/data.txt");

            File.AppendAllText(@dataFile, userData);

            result = "Information saved.";

            countJohn += 1;
        }
    }
 }
    
<!DOCTYPE html>
<html>
<head>
    <title>Elections</title>
</head>
<body>
    <form id="form1" method="post">
        <div>
            <table>
                <tr>
                    <td>Harry</td>
                    <td><input id="Harry" name="submit" type="submit" value="Vote Harry"/></td>

                </tr>
                <tr>
                    <td>John</td>
                    <td><input id="John" name="submit" type="submit" value="Vote John" /></td>
                </tr>
                <tr>
                    <td>Bryan</td>
                    <td><input id="Bryan" name="submit" type="submit" value="Vote Bryan" /></td>
                </tr>
                <tr>
                    <td>Jack</td>
                    <td><input id="Jack" name="submit" type="submit" value="Vote Jack" /></td>
                </tr>
            </table>
        </div>
        <div>
            @if (result != "")
            {
                <p>Result: @result</p>
            }
        </div>
    </form>
</body>
</html>

0 个答案:

没有答案