“創建”部分讓我頭疼
學生控制器.cs
using ContosoUniversity.Data;
using ContosoUniversity.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ContosoUniversity.Controllers
{
public class StudentsController : Controller
{
private readonly SchoolContext _context;
public StudentsController(SchoolContext context)
{
_context = context;
}
public async Task<IActionResult> Index()
{
return View(await _context.Students.ToListAsync());
}
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var student = await _context.Students
.Include(s => s.Enrollments)
.ThenInclude(e => e.Course)
.AsNoTracking()
.FirstOrDefaultAsync(m => m.ID == id);
if (student == null)
{
return NotFound();
}
return View(student);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("EnrollmentDate,FirstMidName,LastName")] Student student)
{
try
{
if (ModelState.IsValid)
{
_context.Add(student);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
}
catch (DbUpdateException /* ex */)
{
//Log the error (uncomment ex variable name and write a log.
ModelState.AddModelError("", "Unable to save changes. "
"Try again, and if the problem persists "
"see your system administrator.");
}
return View(student);
}
}
}
創建.cshtml
@model ContosoUniversity.Models.Student
@{
ViewData["Title"] = "Create";
}
<h1>CreateView</h1>
<h4>Student</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="LastName" class="control-label"></label>
<input asp-for="LastName" class="form-control" />
<span asp-validation-for="LastName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="FirstMidName" class="control-label"></label>
<input asp-for="FirstMidName" class="form-control" />
<span asp-validation-for="FirstMidName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="EnrollmentDate" class="control-label"></label>
<input asp-for="EnrollmentDate" class="form-control" />
<span asp-validation-for="EnrollmentDate" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
如果我洗掉“[HttpPost]”,我的模型會立即更新并使用空資料提交并回傳到索引頁面
實際教程的鏈接:
https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/crud?view=aspnetcore-5.0#update-the-create-page
謝謝
uj5u.com熱心網友回復:
您只需要添加method="post"到<form></form>.
<form asp-action="Create" method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="LastName" class="control-label"></label>
<input asp-for="LastName" class="form-control" />
<span asp-validation-for="LastName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="FirstMidName" class="control-label"></label>
<input asp-for="FirstMidName" class="form-control" />
<span asp-validation-for="FirstMidName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="EnrollmentDate" class="control-label"></label>
<input asp-for="EnrollmentDate" class="form-control" />
<span asp-validation-for="EnrollmentDate" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
這樣表單將包含一個隱藏的輸入,名稱為__RequestVerificationToken,其中包含 AntiForgeryToken 的值。當表單提交時,令牌將傳遞給 Create 操作。
uj5u.com熱心網友回復:
由于該操作具有 [ValidateAntiForgeryToken] 屬性,因此您應該將其添加到視圖中,并將表單標記替換為 html 幫助器
@using (Html.BeginForm("Create", "Students", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
.... and so on
}
恕我直言,不要在您的發布操作中使用任何系結,這絕不是一個好主意
[HttpPost("~/students/create")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(Student student)
uj5u.com熱心網友回復:
找到了解決方案...我需要另一種用于 HttpGet 的方法,具有相同的名稱(創建)...仍然不確定為什么,它現在可以作業,沒有 405 錯誤
如果有人有很好的解釋,請
這是我在控制器中添加的:
public IActionResult Create()
{
return View();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/431747.html
上一篇:物體框架不獲取有關系的行
下一篇:EFCore錯誤加入物體型別名稱
