您好我想将一些数据从输入标签传递到NancyFx中的模块,因此我可以将其添加到json文件中。到处寻找答案却找不到任何答案。
查看
@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<model>
@{
Layout = "Views/Shared/_Layout.cshtml";
}
<div id="addUser">
<form name="addUser" method="POST">
<p>First Name:</p>
<input type="text" name="user">
<input type="submit" name="Submit" value="Submit"/>
</form>
</div>
模块
public class LeaderboardModule : NancyModule
{
public LeaderboardModule()
{
Get["/adduser"] = _ =>
{
//var leaderboard = new LeaderboardModule();
//var users = new List<Users>();
//users = leaderboard.ReadFile("Users.json");
//users.Add(new Users() { Id = 7, Name = "John" });
//var lastItem = users.LastOrDefault();
//leaderboard.WriteFile("Users.json", users);
return View["Shared/_AddUser"];
};
Post["/adduser"] = _ =>
{
var leaderboard = new LeaderboardModule();
var users = new List<Users>();
users = leaderboard.ReadFile("Users.json");
users.Add(new Users() { Id = 7, Name = "John" });
//var lastItem = users.LastOrDefault();
leaderboard.WriteFile("Users.json", users);
return View["Leaderboard"];
};
}
public List<Users> ReadFile(string fileName)
{
var users = new List<Users>();
var readFile = File.ReadAllText(HttpContext.Current.Server.MapPath(fileName));
users = JsonConvert.DeserializeObject<List<Users>>(readFile);
return users;
}
public string WriteFile(string fileName, List<Users> users)
{
var newUser = JsonConvert.SerializeObject(users, Formatting.Indented);
File.WriteAllText(HttpContext.Current.Server.MapPath(fileName), newUser);
return newUser;
}
这里我只是对数据进行硬编码,但我希望能够从视图的输入中添加数据。感谢。
答案 0 :(得分:0)