如何在下一页的表格中列出信息

Posted

技术标签:

【中文标题】如何在下一页的表格中列出信息【英文标题】:how do i list information from a form on the next page 【发布时间】:2021-12-08 16:23:07 【问题描述】:

我正在尝试创建一个表单,用户可以在其中输入他们的信息。他们单击提交,然后在下一页上列出了他们的信息。我创建了两页。创建模板的一个视图页面 (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>

【问题讨论】:

【参考方案1】:

您没有在您的操作中创建任何模型,这就是您出现空引用错误的原因。例如 Review 应该是这样的

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);
        

【讨论】:

【参考方案2】:

我建议您使用提交按钮而不是href(请参阅ASP.NET MVC Form Submit with Link instead of 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 );

【讨论】:

以上是关于如何在下一页的表格中列出信息的主要内容,如果未能解决你的问题,请参考以下文章

在word中怎么使正在编辑的信息永远处于页面最顶端?

我想做一个word,想每页的表格都是一样的

Excel中一页上有很多表格怎样设置为打印出来一页一个表格

如何在下一页打印PDF后拆分表格行并删除标题内容颜色?

excel多个文件批量删除第一列的内容,但保留表头

EXCEL表格如何在每一页最后一行自动求一列的和,要求是自动的,不管是多少行只要够一页它就求和