在这种情况下,如何将数据从视图中的 HTML table 发送到 POST 方法?
How to send data to POST method from HTML table in View in this scenario?
我在 View
的 HTML
table 中显示了一些数据。现在,我想要做的是在单击一个按钮(比如 SUBMIT
按钮时,我想将数据发送到控制器的 POST 方法,以便我可以将数据保存到数据库.
我尝试使用 Model Binding
技术获取数据,但是,POST
方法中的 ViewModel
对象变成了 null
。
模型和视图模型
// Model Model.cs
public class MyModel
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
...
}
// ViewModel MyViewModel.cs
public class MyViewModel
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
...
public List<MyModel> MyModelList { get; set; }
}
// ViewModel VMList.cs
public class VMList
{
public List<MyViewModel> MyViewModelList { get; set; }
}
所以,我有一个名为 MyModel.cs
的 Model
,它在数据库中有 table。然后,我有一个名为 MyViewModel.cs' that has the same columns as the
Model, in addition to some columns, plus a
Listof
MyModeltype. Then, there is another
ViewModelcalled
VMList.csthat contains a list of tuples of
的 ViewModel
MyViewModeltype. This
ViewModelis passed to the
View`。
视图按以下方式构造:
查看
@model ...Models.ViewModels.VMList
...
<form asp-action="MyAction" asp-controller="MyController" id="myForm">
<div>
<button type="submit" value="submit" id="submitButton">Submit</button>
<table id="myTable">
@foreach(var item in Model.MyViewModelList)
{
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Header 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>
@item.FirstName
</td>
<td>
@item.LastName
</td>
<td>
@item.Header3
</td>
</tr>
@if(item.subList != null && item.subList.Count() != 0)
{
<thead>
<tr>
<th>SubList Header 1</th>
<th>SubList Header 2</th>
<th>SubList Header 3</th>
</tr>
</thead>
<tbody>
@foreach(var subItem in item.subList)
{
<tr>
<td>
@subItem.SubListHeader1
</td>
<td>
@subItem.SubListHeader2
</td>
<td>
@subItem.SubListHeader3
</td>
</tr>
}
</tbody>
}
}
</table>
</div>
</form>
POST方法
[HttpPost]
public IActionResult MyAction(VMList vmListObject)
{
return View();
}
如何将 View
中 table 中显示的数据返回到 Controller
?
根据您提供的模型,尝试使用 asp-for
标签助手和 name
属性 进行模型绑定。
@model VMList
<form asp-action="MyAction" asp-controller="HomeController" id="myForm">
<div>
<button type="submit" value="submit" id="submitButton">Submit</button>
<table id="myTable">
@{ int i = 0;}
@foreach (var item in Model.MyViewModelList)
{
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Header 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input asp-for="@item.FirstName" name="vmListObject.MyViewModelList[@i].FirstName" />
</td>
<td>
<input asp-for="@item.LastName" name="vmListObject.MyViewModelList[@i].LastName"/>
</td>
<td>
<input asp-for="@item.Header3" name="vmListObject.MyViewModelList[@i].Header3" />
</td>
</tr>
@if (item.MyModelList != null && item.MyModelList.Count() != 0)
{
<thead>
<tr>
<th>SubList Header 1</th>
<th>SubList Header 2</th>
<th>SubList Header 3</th>
</tr>
</thead>
<tbody>
@{ int j = 0;}
@foreach (var subItem in item.MyModelList)
{
<tr>
<td>
<input asp-for="@subItem.FirstName" name="vmListObject.MyViewModelList[@i].MyModelList[@j].FirstName"/>
</td>
<td>
<input asp-for="@subItem.LastName" name="vmListObject.MyViewModelList[@i].MyModelList[@j].LastName" />
</td>
<td>
test
</td>
</tr>
j++;
}
</tbody>
}
</tbody>
i++;
}
</table>
</div>
</form>
我在 View
的 HTML
table 中显示了一些数据。现在,我想要做的是在单击一个按钮(比如 SUBMIT
按钮时,我想将数据发送到控制器的 POST 方法,以便我可以将数据保存到数据库.
我尝试使用 Model Binding
技术获取数据,但是,POST
方法中的 ViewModel
对象变成了 null
。
模型和视图模型
// Model Model.cs
public class MyModel
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
...
}
// ViewModel MyViewModel.cs
public class MyViewModel
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
...
public List<MyModel> MyModelList { get; set; }
}
// ViewModel VMList.cs
public class VMList
{
public List<MyViewModel> MyViewModelList { get; set; }
}
所以,我有一个名为 MyModel.cs
的 Model
,它在数据库中有 table。然后,我有一个名为 MyViewModel.cs' that has the same columns as the
Model, in addition to some columns, plus a
Listof
MyModeltype. Then, there is another
ViewModelcalled
VMList.csthat contains a list of tuples of
的 ViewModel
MyViewModeltype. This
ViewModelis passed to the
View`。
视图按以下方式构造:
查看
@model ...Models.ViewModels.VMList
...
<form asp-action="MyAction" asp-controller="MyController" id="myForm">
<div>
<button type="submit" value="submit" id="submitButton">Submit</button>
<table id="myTable">
@foreach(var item in Model.MyViewModelList)
{
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Header 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>
@item.FirstName
</td>
<td>
@item.LastName
</td>
<td>
@item.Header3
</td>
</tr>
@if(item.subList != null && item.subList.Count() != 0)
{
<thead>
<tr>
<th>SubList Header 1</th>
<th>SubList Header 2</th>
<th>SubList Header 3</th>
</tr>
</thead>
<tbody>
@foreach(var subItem in item.subList)
{
<tr>
<td>
@subItem.SubListHeader1
</td>
<td>
@subItem.SubListHeader2
</td>
<td>
@subItem.SubListHeader3
</td>
</tr>
}
</tbody>
}
}
</table>
</div>
</form>
POST方法
[HttpPost]
public IActionResult MyAction(VMList vmListObject)
{
return View();
}
如何将 View
中 table 中显示的数据返回到 Controller
?
根据您提供的模型,尝试使用 asp-for
标签助手和 name
属性 进行模型绑定。
@model VMList
<form asp-action="MyAction" asp-controller="HomeController" id="myForm">
<div>
<button type="submit" value="submit" id="submitButton">Submit</button>
<table id="myTable">
@{ int i = 0;}
@foreach (var item in Model.MyViewModelList)
{
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Header 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input asp-for="@item.FirstName" name="vmListObject.MyViewModelList[@i].FirstName" />
</td>
<td>
<input asp-for="@item.LastName" name="vmListObject.MyViewModelList[@i].LastName"/>
</td>
<td>
<input asp-for="@item.Header3" name="vmListObject.MyViewModelList[@i].Header3" />
</td>
</tr>
@if (item.MyModelList != null && item.MyModelList.Count() != 0)
{
<thead>
<tr>
<th>SubList Header 1</th>
<th>SubList Header 2</th>
<th>SubList Header 3</th>
</tr>
</thead>
<tbody>
@{ int j = 0;}
@foreach (var subItem in item.MyModelList)
{
<tr>
<td>
<input asp-for="@subItem.FirstName" name="vmListObject.MyViewModelList[@i].MyModelList[@j].FirstName"/>
</td>
<td>
<input asp-for="@subItem.LastName" name="vmListObject.MyViewModelList[@i].MyModelList[@j].LastName" />
</td>
<td>
test
</td>
</tr>
j++;
}
</tbody>
}
</tbody>
i++;
}
</table>
</div>
</form>