首页 > 解决方案 > 我想将输入从 html 保存到 txt 文件(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>
}

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

解决方案


推荐阅读