【发布时间】:2019-10-03 03:59:51
【问题描述】:
我想像这样更改创建表条件:
有一种通过列表模型创建表格的方法。我的目标是,当在Determine Number文本框中输入一个数字时,表格会根据Determine Number改变。
示例:如果DetermineNumber 为5,则该表的行为5,该表的结构将与旧表相同(A 列0 和B 列0++)。
这是我的代码:
//My Home Controller
using Microsoft.AspNetCore.Mvc;
using MyWeb.Models;
using System.Collections.Generic;
namespace MyWeb.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
var send_class = new GetChecking();
var send_list = new List<MyClass>();
send_class.DetermineNumber = 10;
for (int i = 0; i < send_class.DetermineNumber; i++)
{
send_list.Add(new MyClass { A = 0, B = i });
}
send_class.GetMyList = send_list;
return View(send_class);
}
}
}
//My Class
using System.Collections.Generic;
namespace MyWeb.Models
{
public class GetChecking
{
public int DetermineNumber { get; set; }
public List<MyClass> GetMyList { get; set; }
}
public class MyClass
{
public double A { get; set; }
public double B { get; set; }
}
}
最后,这是我的 Index.cshtml:
<!-- My Index.cshtml -->
@model GetChecking
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div align="center">
<table id="tablex">
<tbody>
<tr>
<td>Tablex Row Count</td>
<td asp-for="@Model.DetermineNumber">@Html.TextBoxFor(m => Model.DetermineNumber)</td>
</tr>
</tbody>
</table>
</div>
<p></p>
<div align="center">
<table id="tablex">
<thead>
<tr><th colspan="2">My Main</th></tr>
<tr>
<th colspan="1">A</th>
<th colspan="1">B</th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.DetermineNumber; i++)
{
<tr>
<td asp-for="@Model.GetMyList[i].A">@Html.TextBoxFor(m => Model.GetMyList[i].A)</td>
<td asp-for="@Model.GetMyList[i].B">@Html.TextBoxFor(m => Model.GetMyList[i].B)</td>
</tr>
}
</tbody>
</table>
</div>
</body>
</html>
【问题讨论】:
-
您的要求不明确!请进一步解释!
标签: asp.net-mvc asp.net-core model-view-controller asp.net-ajax model-binding