【发布时间】: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,有什么更新吗?如果我的回答对你有帮助,记得采纳为答案。
标签: asp.net asp.net-core asp.net-mvc-3