繁体   English   中英

输出不进入.txt文件。 CS HTML

[英]Output not going into .txt file. CS HTML

我在下面创建了一个数组来存储所需的信息。 我的预期输出应该是smth这样的:
约翰,2
5月3日

只要用户按下特定的按钮ID,名称旁边的值就应该递增并替换旧值本身。

不知道为什么信息没有写到data.txt文件中。 请指教。 谢谢。

 @{ var result = ""; if (IsPost) { char[] delimiterChar = { ',' }; var dataFile = Server.MapPath(@"~/App_Data/data.txt"); string[] votesArr = File.ReadAllLines(dataFile); if (votesArr == null) { // Empty file. result = "The file is empty."; } string toWrite = ""; for (int i = 0; i < votesArr.Length - 2; i += 2) { if (votesArr[i].Equals("Harry")) // Equals here is hardcoded, replace with parameter { votesArr[i + 1] = "" + (Int32.Parse(votesArr[i + 1]) + 1); } else if (votesArr[i].Equals("John")) // Equals here is hardcoded, replace with parameter { votesArr[i + 1] = "" + (Int32.Parse(votesArr[i + 1]) + 1); } else if (votesArr[i].Equals("May")) // Equals here is hardcoded, replace with parameter { votesArr[i + 1] = "" + (Int32.Parse(votesArr[i + 1]) + 1); } else if (votesArr[i].Equals("Jane")) // Equals here is hardcoded, replace with parameter { votesArr[i + 1] = "" + (Int32.Parse(votesArr[i + 1]) + 1); } toWrite += votesArr[i] + votesArr[i + 1]; } File.WriteAllText(dataFile, toWrite); result = "Information is saved."; } } 
 <!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> 

您不解析HTMl文件。 您只是将其直接读入数组。

因此,数组中的每一行也将包含HTML语法,但是您没有进行检查,只是在执行.Equals(“ Harry”),所以它永远不会等于“ Harry”,因为它被HTML标记包围了。

您需要使用诸如Angle Sharp之类的东西来解析HTML并提取文本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM