夜行含光
2022-04-20 21:58
采纳率: 100%
浏览 43

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条回答 默认 最新

相关推荐 更多相似问题