我有一些数据,我在HTML
的View
表中显示。现在,我想要做的是点击按钮(比如一个SUBMIT
按钮,我想将数据发送到Controller的POST方法,以便我可以将数据保存到数据库中。
我试图使用Model Binding
技术获取数据,然而,ViewModel
方法中的POST
对象出来是null
。
模型和ViewModel
// 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; }
}
所以,我有一个名为Model
的MyModel.cs
,它是数据库中的表。然后,我有一个名为ViewModel
ModelMyViewModel.cs' that has the same columns as the
List, in addition to some columns, plus a
MyModelof
ViewModeltype. Then, there is another
VMList.cscalled
MyViewModelthat contains a list of tuples of
ViewModeltype. This
View`的is 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
表格中显示的数据恢复为Controller
?
根据您给出的模型,尝试使用asp-for
tag helper和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>