夜行含光 2022-04-20 21:58 采纳率: 100%
浏览 54
已结题

ASP.Net 网页输入的数据如何导入数据库

根据微软官方这篇教程来的https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/intro?view=aspnetcore-6.0&tabs=visual-studio
在搭建机架部分可能是因为数据结构有问题导致失败,自行进行了如下操作(如图,因为是想先测试一下能不能跑所以制作了register页面,对应教程中的create页面,user对应教程中的student)

img


以下是Register.cshtml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using MVer.Models;

namespace MVer.Pages.User
{
    public class RegisterModel : PageModel
    {
        private readonly MVer.Models.MVerContext _context;

        public RegisterModel(MVer.Models.MVerContext context)
        {
            _context = context;
        }

        public IActionResult OnGet()
        {
            User = new Models.User
            {
                Name = "UserName",
                Email = "Your Email",
                Password = "Your Password",
                RegisterDate = DateTime.Now.AddYears(-1)
            };
            return Page();
        }

        [BindProperty]
        public Models.User User { get; set; }

        #region snippet_OnPostAsync
        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }
            #region snippet_TryUpdateModelAsync

            var emptyUser = new Models.User();

            if (await TryUpdateModelAsync<Models.User>(
                emptyUser,
                "User",   // Prefix for form value.
                s => s.Name, s => s.Email, s => s.Password, s => s.RegisterDate))
            {
                #endregion
                _context.Users.Add(emptyUser);
                await _context.SaveChangesAsync();
                return RedirectToPage("./Index");
            }

            return null;
        }
        #endregion
    }
}

以下是Register.cshtml

<h2>Register</h2>

<h4>User</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label  asp-for="User.Name"  class="control-label"></label>
                <input asp-for="User.Name" class="form-control" />
                <span asp-validation-for="User.Name" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="User.Email" class="control-label"></label>
                <input asp-for="User.Email" class="form-control" />
                <span asp-validation-for="User.Email" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="User.Password" class="control-label"></label>
                <input asp-for="User.Password" class="form-control" />
                <span asp-validation-for="User.Password" class="text-danger"></span>
            </div>
            <div class="form-group"> 
                <label asp-for="User.RegisterDate" class="control-label"></label>
                <input asp-for="User.RegisterDate" class="form-control" />
                <span asp-validation-for="User.RegisterDate" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-page="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

现在页面可以正常打开,但是存不上数据,另外在html文件中只能用form来输入信息吗?原本用的是table但是看教程里用的是form就临时改成form了。

  • 写回答

2条回答 默认 最新

  • Rena Ni 2022-04-21 14:23
    关注

    首先你所谓的存不上数据是否有任何报错信息,建议你在后端加断点调试一下找出哪一行出错并且出什么错。

    其次你说的form输入信息,form输入信息直接submit就可以将数据提交到后台,至于你说的table,你要用table的话就要借助js获取值并用ajax传递到后端,会比较繁琐。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月4日
  • 已采纳回答 4月26日
  • 创建了问题 4月20日

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误