在列表中添加表单数据(点网核心)

Posted

技术标签:

【中文标题】在列表中添加表单数据(点网核心)【英文标题】:Add form data in list(dot net core) 【发布时间】:2021-09-06 00:52:54 【问题描述】:

我正在尝试将表单数据添加到列表中,并通过控制器将其显示在表格中以查看但无法访问这些值。我已经为表单视图学生模型类和 studentrepo 和 istudentrepo 创建了一个索引文件,它返回列表但是如何在该列表中添加表单值以便它们呈现。下面是我的代码

 private readonly IStudentRepository _studentRepository;

        public HomeController(ILogger<HomeController> logger, IStudentRepository studentRepository)
        
            _logger = logger;
            _studentRepository = studentRepository;
        

        public ActionResult Index()
           
            return View();
        
        
        public ActionResult StudentDetails()
        
            var data = _studentRepository.GetStudents();
            return View(data);
        
Student details - 
@
    ViewBag.Title = "StudentDetails";

@model List<DotNetdemo.Models.Student>
<div>
    <table id="tab" class="table">
        <thead>
            <tr>

                <th scope="col">Name</th>
                <th scope="col">School</th>
                <th scope="col">Address</th>
                <th scope="col">Gender</th>
                <th scope="col">Standard</th>

            </tr>
            @foreach(var item in Model)
            
                <tr>
                    <td>@item.name</td>
                    <td>@item.school</td>
                    <td>@item.address</td>
                    <td>@item.gender</td>
                    <td>@item.standard</td>
                </tr>
Index - 
@model DotNetdemo.Models.Student
<div class="card">
        <form asp-controller="Home" asp-action="StudentDetails" method="post">
        <div class="heading">
            <h2>REGISTRATION FORM</h2>
        </div>
        <div class="row mb-3">
            
            <label asp-for="name" class="col-sm-3 col-form-label"></label>
            <div class="col-sm-9">
                <input asp-for="name" class="form-control"/>
                <span style="color:red" id="username"></span>
            </div>
        </div>
        <div class="row mb-3">
            <label asp-for="school" class="col-sm-3 col-form-label"></label>
            <div class="col-sm-9">
                <input asp-for="school" class="form-control" />
            </div>
        </div>

【问题讨论】:

你在控制器端的StudentDetails()方法中得到学生列表了吗?在您运行项目时。另外,尝试使用 StudentList 类而不是直接 Var 数据变量。 是的,列表中的虚拟数据正在显示,但是如何从表单中提取值然后添加到列表中 嗨@SanmeetSingh,有什么更新吗?如果我的回答对你有帮助,记得采纳为答案。 【参考方案1】:

这是一个完整的工作演示,您可以关注:

型号:

public class Student

    public int Id  get; set; 
    public string name  get; set; 
    public string school  get; set; 
    public string address  get; set; 
    public string gender  get; set; 
    public string standard  get; set; 

Index.cshtml

@model Student

<div class="row">
    <div class="col-md-4">
        <form asp-controller="Home" asp-action="StudentDetails" method="post">
            <div class="heading">
                <h2>REGISTRATION FORM</h2>
            </div>
            <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="school" class="control-label"></label>
                <input asp-for="school" class="form-control" />
                <span asp-validation-for="school" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="address" class="control-label"></label>
                <input asp-for="address" class="form-control" />
                <span asp-validation-for="address" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="gender" class="control-label"></label>
                <input asp-for="gender" class="form-control" />
                <span asp-validation-for="gender" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="standard" class="control-label"></label>
                <input asp-for="standard" class="form-control" />
                <span asp-validation-for="standard" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

StudentDetails.cshtml:

@model List<Student>
<div>
    <table id="tab" class="table">
        <thead>
            <tr>

                <th scope="col">Name</th>
                <th scope="col">School</th>
                <th scope="col">Address</th>
                <th scope="col">Gender</th>
                <th scope="col">Standard</th>

            </tr>
            @foreach (var item in Model)
            
                <tr>
                    <td>@item.name</td>
                    <td>@item.school</td>
                    <td>@item.address</td>
                    <td>@item.gender</td>
                    <td>@item.standard</td>
                </tr>
            
        </thead>
    </table>
</div>

回购:

public interface IStudentRepository

    List<Student> GetStudents();
    Task CreateStudent(Student student);


public class StudentRepository : IStudentRepository

    private readonly YourDbcontext _context;
    public StudentRepository(YourDbcontext context)
    
        _context = context;
    

    public async Task CreateStudent(Student student)
    
        _context.Add(student);
        await _context.SaveChangesAsync();
    
    public List<Student> GetStudents()
    
        var students =  _context.Student.ToList();
        return students;
    

控制器:

public class HomeController : Controller

    private readonly ILogger<HomeController> _logger;
    private readonly IStudentRepository _studentRepository;
    public HomeController(IStudentRepository studentRepository, ILogger<HomeController> logger)
    
        _logger = logger;
        _studentRepository = studentRepository;
    

    public IActionResult Index()
    
        return View();
    
    public async Task<IActionResult> StudentDetails()
    
        var data = _studentRepository.GetStudents();
        return View(data);
    
    [HttpPost]
    public async Task<IActionResult> StudentDetails(Student student)
    
        await _studentRepository.CreateStudent(student);
        return RedirectToAction(nameof(StudentDetails));
    

【讨论】:

以上是关于在列表中添加表单数据(点网核心)的主要内容,如果未能解决你的问题,请参考以下文章

在 laravel 表单中添加默认值以选择列表

是否可以将列表中的值添加到输入表单字段?

检索数据表单数据库后列表视图不更新

添加列表框项目在 xamarin 表单中不起作用

数据表编辑器 - 如何在编辑器表单中动态填充选择列表?

ASP.NET Razor - 如何为对象列表创建 POST 表单?