我想将输入从html保存到txt文件(ASP.NET MVC)

时间:2019-08-31 16:48:07

标签: c# asp.net asp.net-mvc

我想创建一个注册站点,它将接收用户信息并将其保存到文本文件(它必须是文本文件,而不是数据库)。

这是我的控制者:

public IActionResult Register()
{
    UserRLModel cos = new UserRLModel();
    string root = @"D:\dotNET\RiderProjects\WebApplication3\UserInfo\Users.txt";
    using (StreamWriter sw = new StreamWriter(root))
    {
        sw.WriteLine(cos.Name);
    };
    return View(cos.Name);
}

这是我的模特

using System;
namespace WebApplication3.Models
{
    public class UserRLModel
    {
        public string Name { get; set; }
        public string Password { get; set; }
        public string Email { get; set; }
        public DateTime Date { get; set; }
    }
}

这是我的观点:

@model WebApplication3.Models.UserRLModel

@{
    ViewBag.Title = "Register";
    Layout = "_Layout";
}
@using (Html.BeginForm("Register", "UserRL", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()
    <h4>Wprowadź dane użytkownika</h4>
    <hr />
    @Html.ValidationSummary("", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => Model.Name, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(model => Model.Name, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Dodaj" class="btn btn-default" />
            @Html.ActionLink("Anuluj", "Index", null, htmlAttributes: new { @class = "btn btn-danger" })
        </div>
    </div>
}

0 个答案:

没有答案