我正在嘗試創建一個表單,用戶可以在其中輸入他們的資訊。他們單擊提交,然后在下一頁上列出他們的資訊。我創建了兩個頁面。創建模板的一個視圖頁面 (Book.cshtml)。在另一個視圖頁面 (Review.cshtml) 上,模板是串列。他們的兩個模型類都取自 Form.cs 模型。我的問題是,當我單擊提交時,它給了我這個錯誤,“System.NullReferenceException:'物件參考未設定為物件的實體。'。” 我猜這是因為 Book.cshtml 中的 Form.cs 模型中沒有存盤任何資訊。我如何將用戶輸入的資訊存盤在 Book 頁面上,并顯示在 Review 頁面上。我嘗試使用 [HttpGet] 和 [HttpPost],但沒有發生任何事情。我正在使用 Visual Studio asp.net core MVC 3.1
家庭控制器
using Hotel_Lodge.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace Hotel_Lodge.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
return View();
}
[HttpGet]
public IActionResult Book()
{
return View();
}
[HttpPost]
public IActionResult Book(Form form)
{
return View();
}
public IActionResult Review()
{
return View();
}
public IActionResult Privacy()
{
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
Form.cs 模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Hotel_Lodge.Models
{
public class Form
{
public string Name { get; set; }
public string Arrival { get; set; }
public string Departure { get; set; }
public string RoomType { get; set; }
public string Misc { get; set; }
}
}
Book.cshtml(查看)
@model Hotel_Lodge.Models.Form
@{
ViewData["Title"] = "Book";
}
<h1>Book</h1>
<h4>Form</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Book">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Arrival" class="control-label"></label>
<input asp-for="Arrival" class="form-control" />
<span asp-validation-for="Arrival" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Departure" class="control-label"></label>
<input asp-for="Departure" class="form-control" />
<span asp-validation-for="Departure" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="RoomType" class="control-label"></label>
<input asp-for="RoomType" class="form-control" />
<span asp-validation-for="RoomType" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Misc" class="control-label"></label>
<input asp-for="Misc" class="form-control" />
<span asp-validation-for="Misc" class="text-danger"></span>
</div>
<div>
<a type="submit" class="btn btn-dark" href="https://localhost:44348/Home/Review">Next</a>
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Review.cshtml(查看)
@model IEnumerable<Hotel_Lodge.Models.Form>
@{
ViewData["Title"] = "Review";
}
<h1>Review</h1>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Arrival)
</th>
<th>
@Html.DisplayNameFor(model => model.Departure)
</th>
<th>
@Html.DisplayNameFor(model => model.RoomType)
</th>
<th>
@Html.DisplayNameFor(model => model.Misc)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Arrival)
</td>
<td>
@Html.DisplayFor(modelItem => item.Departure)
</td>
<td>
@Html.DisplayFor(modelItem => item.RoomType)
</td>
<td>
@Html.DisplayFor(modelItem => item.Misc)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</tbody>
</table>
uj5u.com熱心網友回復:
您不會在您的操作中創建任何模型,這就是您有空參考錯誤的原因。例如評論應該是這樣的
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly DbContext _context
public HomeController(
ILogger<HomeController> logger,
DbContext context
)
{
_logger = logger;
_context=context
}
public IActionResult Review()
{
var model= _context.Set<FormModel>().ToList();
return View(model);
}
uj5u.com熱心網友回復:
我建議您使用提交按鈕而不是href(請參閱ASP.NET MVC 表單提交與鏈接而不是 Button)。替換以下行Book.cshtml
<a type="submit" class="btn btn-dark" href="https://localhost:44348/Home/Review">Next</a>
經過
<button type="submit" class="btn btn-primary">Next</button>
因此,單擊Next會將Form物件傳遞給Book(Form form)。
并將Book(Form form)方法更新為:
[HttpPost]
public IActionResult Book(Form form)
{
if (!ModelState.IsValid)
{
return RedirectToAction("Index", form);
}
// You should add the current a new record to a repository here
// and pass an updated list to the `Review`.
// Currently only a new `form` record is passed to the `Review` for demo purpose.
return View("Review", new List<Form> { form });
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/330730.html
標籤:C# asp.net-mvc 视觉工作室 asp.net-mvc-3
上一篇:按類和名稱查找Web元素(按鈕)
下一篇:C#正則運算式如何從字串中排除
